我已经使用 JAXB 解析 XML。如何通过 JAXB xml 解析获取特定元素(即子节点),而不将该元素解析为节点。
<?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>
我已经完成了这样的解组,它工作正常。为了性能,我不希望元素作为节点。还有其他方法吗?
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;
}
}