[英]Exclude specific tags from xml?
我正在使用jsoup通過xmlDoc.select("ns|properties")
從xml文件中提取一些屬性
問題:找到所有出現的“屬性”標簽。 我只想要ns:tests標記之外的屬性。 如何排除它們?
<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>
您可以嘗試以下兩種方法:
/*
* 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);
如果選擇解決方案2 ,請確保備份 (例如克隆) xmlDoc
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.