[英]how to get these XML elements with libxml2?
我有一個XML結構:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<root>
<item>
<foo1>1</foo1>
<foo2>2</foo2>
</item>
<item>
<foo1>3</foo1>
<foo2>4</foo2>
</item>
</root>
現在我去給所有孩子一個for循環:
for (pCurrentElement...) {
}
現在我想訪問將foo1和foo2拋出pCurrentElement,但是我不知道如何。 我使用LIBXML2
我可以通過以下方式獲取foo1:
pChildElement = pCurrentElement->children->next;
pChildElement->children->content // foo1
但我現在不知道如何獲取foo2?
默認情況下,libxml2將文檔中的空格解析為自己的子節點。 如果foo1和foo2前面有空格,則foo1將為pCurrentElement->children->next
,而foo2將為pCurrentElement->children->next->next->next
。
如果禁用空白解析,請使用xmlKeepBlanksDefault(0)
或xmlReadDoc(..., XML_PARSE_NOBLANKS)
,則foo1將是pCurrentElement->children
而foo2將是pCurrentElement->children->next
。
使用xmlNextElementSibling
。 當應用於foo1
,它將為您提供foo2
。
或循環遍歷所有while (child->next)
並僅處理給定type
節點。
pChildElement = pCurrentElement->children->next; //if this is pointing to <item>
pChildElement->children->content // foo1 // this is <item>->children->first element (which is foo1)
很自然
pChildElement->children->next->content would be the next sibling of foo1, which is foo2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.