問(wèn)題描述
我的問(wèn)題是:當(dāng)存在與父元素的孫子"同名的其他元素時(shí),如何直接獲取特定父元素下的元素.
My question is: How can I get elements directly under a specific parent element when there are other elements with the same name as a "grandchild" of the parent element.
我正在使用 Java DOM 庫(kù) 解析 XML 元素 和我'我遇到了麻煩.這是我正在使用的 一些(一小部分)xml:
I'm using the Java DOM library to parse XML Elements and I'm running into trouble. Here's some (a small portion) of the xml I'm using:
<notifications>
<notification>
<groups>
<group name="zip-group.zip" zip="true">
<file location="C:validdirectory" />
<file location="C:anothervalidfile.doc" />
<file location="C:validfilehere.txt" />
</group>
</groups>
<file location="C:validfile.txt" />
<file location="C:validfile.xml" />
<file location="C:validfile.doc" />
</notification>
</notifications>
如您所見(jiàn),您可以在兩個(gè)地方放置 <file>
元素.無(wú)論是在組內(nèi)還是在組外.我真的希望它采用這種結(jié)構(gòu),因?yàn)樗鼘?duì)用戶更友好.
As you can see, there are two places you can place the <file>
element. Either in groups or outside groups. I really want it structured this way because it's more user-friendly.
現(xiàn)在,每當(dāng)我調(diào)用 notificationElement.getElementsByTagName("file");
時(shí),它都會(huì)為我提供所有 <file>
元素,包括 < 下的元素;group>
元素.我以不同的方式處理這些文件中的每一種,所以這個(gè)功能是不可取的.
Now, whenever I call notificationElement.getElementsByTagName("file");
it gives me all the <file>
elements, including those under the <group>
element. I handle each of these kinds of files differently, so this functionality is not desirable.
我想到了兩種解決方案:
I've thought of two solutions:
- 獲取文件元素的父元素并進(jìn)行相應(yīng)處理(取決于是
還是
. - 重命名第二個(gè)
<file>
元素以避免混淆.
- Get the parent element of the file element and deal with it accordingly (depending on whether it's
<notification>
or<group>
. - Rename the second
<file>
element to avoid confusion.
這些解決方案都不像只是讓事情保持原樣并只獲得 <file>
元素,這些元素是 <notification>
的直接子元素元素.
Neither of those solutions are as desirable as just leaving things the way they are and getting only the <file>
elements which are direct children of <notification>
elements.
我愿意接受 IMPO 關(guān)于最佳"方法的評(píng)論和回答,但我對(duì) DOM 解決方案真的很感興趣,因?yàn)檫@就是其余的這個(gè)項(xiàng)目正在使用.謝謝.
I'm open to IMPO comments and answers about the "best" way to do this, but I'm really interested in DOM solutions because that's what the rest of this project is using. Thanks.
推薦答案
嗯,這個(gè)問(wèn)題的 DOM 解決方案其實(shí)很簡(jiǎn)單,雖然不太優(yōu)雅.
Well, the DOM solution to this question is actually pretty simple, even if it's not too elegant.
當(dāng)我遍歷調(diào)用notificationElement.getElementsByTagName("file")
時(shí)返回的filesNodeList
時(shí),我只檢查父節(jié)點(diǎn)的名稱是否為通知"".如果不是,那么我將忽略它,因?yàn)檫@將由 <group>
元素處理.這是我的代碼解決方案:
When I iterate through the filesNodeList
, which is returned when I call notificationElement.getElementsByTagName("file")
, I just check whether the parent node's name is "notification". If it isn't then I ignore it because that will be handled by the <group>
element. Here's my code solution:
for (int j = 0; j < filesNodeList.getLength(); j++) {
Element fileElement = (Element) filesNodeList.item(j);
if (!fileElement.getParentNode().getNodeName().equals("notification")) {
continue;
}
...
}
這篇關(guān)于按名稱僅獲取 XML 直接子元素的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!