Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB解析复杂xml_Java_Xml_Xml Parsing_Jaxb - Fatal编程技术网

Java JAXB解析复杂xml

Java JAXB解析复杂xml,java,xml,xml-parsing,jaxb,Java,Xml,Xml Parsing,Jaxb,我想在客户节点中获取id、年龄、姓名以及列表类型、来自客户节点的联系人。 我的示例Xml格式是 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <customers> <values> <type> <customer id="100"> <age>29</age> <name>mkyong</name> &l

我想在客户节点中获取id、年龄、姓名以及列表类型、来自客户节点的联系人。

我的示例Xml格式是

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customers>
<values>
<type>
<customer id="100">
    <age>29</age>
    <name>mkyong</name>
</customer>
<customer id="140">
    <age>29</age>
    <name>chandoo</name>
</customer>
</type>
</values>
<listType>Population</listType>
<contact>phani</contact>
</customers>
主课 App.java

public static void main(String[] args) {        
        try {
            File file = new File("D:/userxml/complex.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Customers.class,Values.class,Type.class,Customer.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();            
            Type type = (Type) jaxbUnmarshaller.unmarshal(file);            
            System.out.println(type); 

        } catch (JAXBException e) {
            e.printStackTrace();
        }        
    }
我是PHP开发人员,不熟悉java。请帮帮我。我想获取客户详细信息

请使用:

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Customers customers = (Customers) jaxbUnmarshaller.unmarshal(file);
这将使用类中定义的结构创建Customers对象,您可以使用适当的getter获取所需的值

要回答您评论中的问题,请尝试(这只是一个硬编码调用列表中第一个元素的示例):


调用适当的getter并迭代类型中客户对象的列表

你的问题是什么?@BetaRide我编辑了我的问题。您能否提供此查询的帮助url或答案。TQ最初我尝试了这种模式,并从Customers节点获取listType和Contact值。我还想在类型节点的customer节点中获取name、age值。这是我的问题。JAXB将基于xml和您拥有的结构进行解组。请在我的答案中检查我的编辑。您可以通过调用适当的getter(我已经包括了一个示例)来获取值。是的,但是我没有在内部客户节点中获取名称、年龄。请粘贴我在回答中输入的sysout命令的输出。我可以运行这段代码并查看name、age等。问题是我在Type.java类中为getCustomer添加了一个操作(@xmlementwrapper(name=“customer”))。如果我想循环姓名和年龄,我该怎么做。很多。
class Type {    
    private int id;
    private List<Customer> customer;
    Setters & Getters
}
class Customer {    
    private String name;
    private int age;
    Setters & Getters
}
public static void main(String[] args) {        
        try {
            File file = new File("D:/userxml/complex.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Customers.class,Values.class,Type.class,Customer.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();            
            Type type = (Type) jaxbUnmarshaller.unmarshal(file);            
            System.out.println(type); 

        } catch (JAXBException e) {
            e.printStackTrace();
        }        
    }
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Customers customers = (Customers) jaxbUnmarshaller.unmarshal(file);
        System.out.println(customers.getValues().get(0).getType().get(0));
        System.out.println(customers.getValues().get(0).getType().get(0).getCustomer().get(0).getAge());
        System.out.println(customers.getValues().get(0).getType().get(0).getCustomer().get(0).getName());