java 如何通过 JAXB xml 解析获取特定元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14358769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a particular element through JAXB xml parsing?
提问by thilo
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.
我已经使用 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>
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);
Object model is like
对象模型就像
@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;
}
}
}
采纳答案by bdoughan
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.
可以使用StAX (JSR-173)解析器(包含在从 Java SE 6 开始的 JDK/JRE 中)。然后您可以将 推进XMLStreamReader
到子节点并从那里解组。
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();
}
}
For More Information
想要查询更多的信息
回答by Narendra Pathai
You will have to do following code for doing JAXB Unmarshalling:
您必须执行以下代码才能执行JAXB Unmarshalling:
unmarshaler.unmarshal(source);
This will return you the instanceof class which you would have generated using xjc command.
这将返回您使用 xjc 命令生成的类的实例。
Then you can access any property using bean methods.
然后您可以使用 bean 方法访问任何属性。