我有一个包含XML的org.w3c.dom.Document
,如果我想从该文档中获取某些标签中包含的一些信息,我该怎么办?
示例(我只希望<catalog>
<catalog/>
中包含标记)
//Some VALUES
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
<ORACLE>
select * from scott.dept;
</ORACLE>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<ORACLE>
begin
htp.p('This is the test data');
end;
</ORACLE>
</CD>
</CATALOG>
//OTHER VALUES
我发现Document有方法getElementsByTag名称是一个NodeList,我怎么能在Document中转换它?
答案 0 :(得分:1)
我建议编写一个XPath expression,它返回您要删除的所有节点,然后遍历结果并从DOM中删除节点。
演示(未经测试,我也假设您的根元素称为ROOT
):
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/ROOT/CATALOG/CD/*[self::COMPANY or self::PRICE]";
NodeList nodes = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
for (Node n: nodes) {
n.getParentNode().removeChild(n);
}
答案 1 :(得分:0)
我使用过这段代码:
Node elem = null;
NodeList lista=doc.getElementsByTagName("Items");
for(int i=0;i<lista.getLength();i++){
elem = lista.item(i);
}
NodeList lista2=doc.getElementsByTagName("MoreOffersUrl");
for(int i=0;i<lista2.getLength();i++){
Node elem2=lista2.item(i);
elem.removeChild(elem2);
}