How to get a particular element through JAXB xml parsing?(如何通过 JAXB xml 解析获取特定元素?)
问题描述
我已经使用 JAXB 来解析 XML.如何通过 JAXB xml 解析获取特定元素(即子节点)而不将该元素解析为节点.
I have used JAXB to parse an XML.How to get a particular element(ie a child node) through JAXB xml parsing without parsing that element as node.
<?xml version="1.0" encoding="UTF-8"?>
<Header>
<From>
<Credential
domain="NetworkId"><Identity>ANXXXNNN</Identity>
</Credential>
</From>
<To>
<Credential
domain="NetworkId"><Identity>ANNNXXXXXT</Identity>
</Credential>
</To>
<To>
<Credential
domain="NetworkId"><Identity>BNNXXXT</Identity>
</Credential>
</To>
</Header>
我已经完成了这样的解组,它工作正常.为了性能,我不希望元素作为节点.还有其他方法吗?
I have done unmarshalling like this,It works fine.For performance,I dont want the elements as node.Is there anyother way to do?
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc;
doc = db.parse(file);
NodeList node = (NodeList)doc.getElementsByTagName("TO");
JAXBElement<ToType> element = jaxbUnmarshaller.unmarshal(node.item(0),ToType.class);
对象模型就像
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ToType", propOrder = {
"credential"
})
public class ToType {
@XmlElement(name = "Credential", required = true)
protected CredentialType credential;
public CredentialType getCredential() {
return credential;
}
public void setCredential(CredentialType value) {
this.credential = value;
}
}
推荐答案
A <可以使用strong>StAX (JSR-173) 解析器(包含在从Java SE 6 开始的JDK/JRE 中).然后您可以将 XMLStreamReader
推进到子节点并从那里解组.
A StAX (JSR-173) parser (included in the JDK/JRE starting with Java SE 6) can be used. Then you can advance the XMLStreamReader
to the child node and unmarshal from there.
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xml = new StreamSource("src/forum14358769/input.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
// Advance to the "To" element.
while(xsr.hasNext()) {
if(xsr.isStartElement() && "To".equals(xsr.getLocalName())) {
break;
}
xsr.next();
}
// Unmarshal from the XMLStreamReader that has been advanced
JAXBContext jc = JAXBContext.newInstance(ToType.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
ToType toType = unmarshaller.unmarshal(xsr, ToType.class).getValue();
}
}
更多信息
- http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
这篇关于如何通过 JAXB xml 解析获取特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何通过 JAXB xml 解析获取特定元素?
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01