我正在解析一个XML
与 JAXB 但XML
最后有一条评论,我想解析它来存储它。
Xml:
<xml>...</xml>
<!--RUID: [UmFuZG9tSVYkc2RlIyh9YUMeu8mgftUJQvv83JiDhiMR==] -->
我需要获取评论的字符串。
JAXB 有给我评论的功能吗?
Jaxb 活页夹允许您阅读注释,如 Blaise Doughan 在此记录的那样http://bdoughan.blogspot.com/2010/09/jaxb-xml-infoset-preservation.html http://bdoughan.blogspot.com/2010/09/jaxb-xml-infoset-preservation.html.
要获取特定元素下方的评论,请使用例如
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(yourFile);
JAXBContext jc = JAXBContext.newInstance(YourType.class.getPackage().getName());
Binder<Node> binder = jc.createBinder();
JAXBElement<YourType> yourWrapper = binder.unmarshal(document, YourType.class);
YourType rootElement = yourWrapper.getValue();
// Get comment below root node
Node domNode = binder.getXMLNode(rootElement);
Node nextNode = domNode.getNextSibling();
if (nextNode.getNodeType() == Node.COMMENT_NODE) {
comment = nextNode.getTextContent();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)