简体   繁体   English

如何在JAXB的帮助下处理嵌套的xml标记?

[英]How I can to process a nested xml tags with the help of JAXB?

How can I to process such structure with the help of JAXB? 如何在JAXB的帮助下处理这种结构? There is one parent root catalog and many other in it, that are keeping info about many objects (states, for example). 有一个父根目录,其中还有许多其他根目录,其中包含有关许多对象(例如状态)的信息。

    <?xml version='1.0' encoding='utf-8'?>
    <root>
        <states>
            <state>
                <name>Alabama</name>
                <id>al</id>
            </state>
            <state>
                <name>Alaska</name>
                <id>ak</id>
            </state>
            ...
        </states>
        <airports>
            <airport>
                ...
            </airport>
            <airport>
                ...
            </airport>
            ...
        </airports>
        ...
    </root>

In case states, I created class States: 在情况状态下,我创建了状态状态:

    @XmlRootElement(name = "root")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class States {
        @XmlElement(name="states")
        private List<State> states;

        public List<State> getBrokers(){
            return states;
        }

        public void setBrokers(List<State> states) {
            this.states = states;
        }
    }

and class State: 和状态:

@XmlAccessorType(XmlAccessType.FIELD)
public class State {
    @XmlElement(name = "name")
    private String name;

    @XmlElement(name = "id")
    private String id;
    public State(){}
    public State(String id, String name) {
        this.id = id;
        this.name = name;
}

But when I call the States object 但是当我称国家为对象时

public class JaxbParser {
    public Object getObject(File file, Class c) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Object object = unmarshaller.unmarshal(file);

        return object;
    }

    public void saveObject(File file, Object o) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(o.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(o,file);
    }
}

public static void main(String[] args) throws JAXBException {
    JaxbParser parser = new JaxbParser();
    States brol =  (States) parser.getObject(new File("C:\\Users\\...\\1.xml"), States.class);

    List<State> brokerList = brol.getStates();
    System.out.println(brol);
}

the stateList contains only one state with id and name equals null. stateList仅包含一个ID为id且名称等于null的状态。 What am I doing wrong ? 我究竟做错了什么 ?

public static void main(String args[]) throws 
JAXBException {
 try {
    File file = new File("....xml");
    JAXBContext jaxbContext = 
    JAXBContext.newInstance(Classname.class);
    Unmarshaller jaxbUnmarshaller = 
     jaxbContext.createUnmarshaller();
    Classname  objectName = (Classname) 
    jaxbUnmarshaller.unmarshal(file);

    List<Classname> list =  objectName.getStates();
    int i = 1;
    for (State ans : list){
        .....
    }

 } catch (JAXBException e) {
    e.printStackTrace();
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM