我已经使用JAXB来解析XML.How打通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>
我做了解组这样的,它的工作原理fine.For的表现,我不想要的元素node.Is有任何其它的办法吗?
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;
}
}