使用jaxb将复杂xml解析为java

使用jaxb将复杂xml解析为java,jaxb,Jaxb,谁能推荐一段代码,用于使用jaxb将xml转换为java。 我的xml文件看起来像 <Customer> <Operation>Sample</Operation> <head> <sub> <value> <att> <req>

谁能推荐一段代码,用于使用jaxb将xml转换为java。 我的xml文件看起来像

     <Customer>
    <Operation>Sample</Operation>
    <head>
        <sub>
            <value>
                <att>
                    <req>
                        <name>sample</name>
                        <value>
                            <string> sample values </string>
                        </value>
                    </req>
                </att>
            </value>
        </sub>
        <sub>
            <value>
                <att>
                    <req>
                        <name>var</name>
                        <value>
                            <string>var value</string>
                        </value>
                    </req>
                </att>
            </value>
        </sub>
    </head>
</Customer>
我的预期输出应该是

Sample
sample values
var
var value

如何通过相同的标记名获得多个值。例如,如果xml是

Xml文件

   <customer>
      <sample>
      <name>name</name>
      <value>
      <string> value </string>
      </value> 
      </sample>
      <sample>
      <name>name</name>
      <value>
      <string> value </string>
      </value> 
      </sample>
</customer>

您可能对以下内容感兴趣:
   <customer>
      <sample>
      <name>name</name>
      <value>
      <string> value </string>
      </value> 
      </sample>
      <sample>
      <name>name</name>
      <value>
      <string> value </string>
      </value> 
      </sample>
</customer>
File file = new File("C:\\sample.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer.value.string);
System.out.println(customer.sample.name);