[英]Exclude specific tags from xml?
I'm using jsoup to extract some properties from an xml file with xmlDoc.select("ns|properties")
我正在使用jsoup通过
xmlDoc.select("ns|properties")
从xml文件中提取一些属性
Problem: it finds all occurences of "properties" tag. 问题:找到所有出现的“属性”标签。 I only want the properties outside the ns:tests tags.
我只想要ns:tests标记之外的属性。 How can I exclude them?
如何排除它们?
<ns:interface>
</ns:interface>
<ns:tests>
<ns:properties>
<ns:name>name</ns:name>
<ns:id>2</ns:id>
</ns:properties>
</ns:test>
<ns:properties>
<ns:name>name</ns:name>
<ns:id>1</ns:id>
</ns:properties>
You can try these two ways: 您可以尝试以下两种方法:
/*
* Solution 1: Check if a 'ns:properties' is inside a 'ns:tests'
*/
for( Element element : xmlDoc.select("ns|properties") )
{
if( element.parent() != null && !element.parent().tagName().equals("ns:tests") )
{
/* Only elements outside 'ns:tests' here */
System.out.println(element);
}
}
/*
* Solution 2: removing all 'ns:tests' elements (including all inner nodes.
*
* NOTE: This will DELETE them from 'xmlDoc'.
*/
xmlDoc.select("ns|tests").remove();
Elements properties = xmlDoc.select("ns|properties");
System.out.println(properties);
If you choose Solution 2 , make shure you backup (eg. clone) xmlDoc
. 如果选择解决方案2 ,请确保备份 (例如克隆)
xmlDoc
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.