read Xml file with JAXB

前端 未结 2 550
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 16:20

I want to read a xml file (like below) but I get an Execption. Could you please help me how can I fix this problem?



        
相关标签:
2条回答
  • 2021-01-02 16:49

    When you describing an XML model, you need to begin with a root entity (in your case it's the<config> element).

    @XmlRootElement(name="config")
    class Config implements Serializable {
       private Log log;
       private Env env;
    
       @XmlElement(name="log")
       public Log getLog() {
          return this.log;
       }
    
       @XmlElement(name="env")
       public Env getEnv() {
          return this.env;
       }
    
       // Setters are omitted
    }
    

    And then you parse the XML like the following:

    JAXBContext jaxbContext = JAXBContext.newInstance(Config.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Config config = (Config) jaxbUnmarshaller.unmarshal(file);
    if (config != null && config.getLog() != null) {
       customer = config.getLog().getProperties();
    }
    
    0 讨论(0)
  • 2021-01-02 17:01
    javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"config"). Expected elements are (none)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:631)
    

    You need to ensure that you associate a class with the root element of the XML document using @XmlRootElement or @XmlElementDecl (see: http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html). Alternatively you can use one of the unmarshal methods that take a Class parameter to tell JAXB what type of object you are unmarshalling.


    Domain Model (Config)

    I would recommend having a domain class like the following from which you could obtain the two lists of Property objects.

    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Config {
    
        private List<Property> logProperties = new ArrayList<Property>();
        private List<Property> envProperties = new ArrayList<Property>();
    
        @XmlElementWrapper(name="log")
        @XmlElement(name="property")
        public List<Property> getLogProperties() {
            return logProperties;
        }
    
        @XmlElementWrapper(name="env")
        @XmlElement(name="property")
        public List<Property> getEnvProperties() {
            return envProperties;
        }
    
    }
    

    Demo

    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Config.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum17059227/input.xml");
            Config config = (Config) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "file:///C:/Documents%20and%20Settings/mojalal/Desktop/FirstXSD.xml");
            marshaller.marshal(config, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Documents%20and%20Settings/mojalal/Desktop/FirstXSD.xml">
        <env>
            <property key="firstenv" value="fo"/>
            <property key="123" value="333"/>
        </env>
        <log>
            <property key="firstKey" value="firstValue"/>
            <property key="secoundKey" value="secoundKey"/>
            <property key="thirdKey" value="thirdValue"/>
        </log>
    </config>
    
    0 讨论(0)
提交回复
热议问题