使用 JAXB 在 Java 中解组 xml 属性和值

我有一个来自 REST API 的 XML 响应,如下所示:


 <?xml version="1.0" encoding="UTF-8"?>

 <ns2:testpla xmlns:ns2="http:xyz"  xmlns:ns7="xyz">

 <ns2:category term="Default Category" value="Default Category Value"/>

 <ns2:testase ns7:resource="https://www.cyz.com" units="PH" 

  href="ww.com">XYZ</ns2:testase>

 <ns2:testase ns7:resource="https://ww.cyz.com" units="LH" 

  href="ww.org">AZ</ns2:testase>

 <com.abc xmlns="http://lq.net" extensionDisplayName="QWZ-KEY-TP-TEST-ZWE- 

 TI">

  <div xmlns="http://www.w3.org/1999/xhtml">TriggerA ND confirm the 

 functionality</div>

  </com.abc>

  </ns2:testpla>

我知道如何使用 jaxb 获取 xml 元素值,即“XYZ”并绑定到 bean。但我坚持知道如何获取资源的值(即;“ https://www.cyz.com ”),units(“PH”),href(“ww.com”),xmlns 的值div ? 然后将值映射到对象属性。请帮我。


慕丝7291255
浏览 114回答 1
1回答

小唯快跑啊

为 testpla 和 testase 创建单独的两个类Testpla.java@XmlRootElement(name = "ns2:testpla")public class Testpla {&nbsp; &nbsp; private Testase testase;&nbsp; &nbsp; public Testase getTestase() {&nbsp; &nbsp; &nbsp; &nbsp; return testase;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElement(name = "ns2:testase")&nbsp; &nbsp; public void setTestase(Testase testase) {&nbsp; &nbsp; &nbsp; &nbsp; this.testase = testase;&nbsp; &nbsp; }}Testase.java@XmlRootElement(name = "ns2:testase")public class Testase {&nbsp; &nbsp; private String resource;&nbsp; &nbsp; private String units;&nbsp; &nbsp; public String getResource() {&nbsp; &nbsp; &nbsp; &nbsp; return resource;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlAttribute(name = "ns7:resource")&nbsp; &nbsp; public void setResource(String resource) {&nbsp; &nbsp; &nbsp; &nbsp; this.resource = resource;&nbsp; &nbsp; }&nbsp; &nbsp; public String getUnits() {&nbsp; &nbsp; &nbsp; &nbsp; return units;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlAttribute(name = "units")&nbsp; &nbsp; public void setUnits(String units) {&nbsp; &nbsp; &nbsp; &nbsp; this.units = units;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java