久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

按名稱僅獲取 XML 直接子元素

Get XML only immediate children elements by name(按名稱僅獲取 XML 直接子元素)
本文介紹了按名稱僅獲取 XML 直接子元素的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(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:

  1. 獲取文件元素的父元素并進(jìn)行相應(yīng)處理(取決于是還是.
  2. 重命名第二個(gè) <file> 元素以避免混淆.
  1. Get the parent element of the file element and deal with it accordingly (depending on whether it's <notification> or <group>.
  2. 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進(jìn)度偵聽(tīng)器未觸發(fā)(Google 驅(qū)動(dòng)器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無(wú)效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅(qū)動(dòng)api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動(dòng)使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號(hào)返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 99久久精品国产毛片 | 视频一区二区三区中文字幕 | 一区二区视频在线 | 久久成人精品视频 | 一区二区三区高清 | 国产精品免费一区二区三区四区 | 国产一区二区在线免费视频 | 激情av网站 | 美国黄色毛片 | 91精品国产一区二区三区蜜臀 | 欧美操操操 | 成年女人免费v片 | 成人免费看片又大又黄 | 久久婷婷色| 精区3d动漫一品二品精区 | 伊伊综合网 | 欧美色成人 | 在线观看国产视频 | 成人免费观看网站 | 久久久久久久久久久久亚洲 | 真人一级毛片 | 中文字幕综合 | 国产精品99久久久精品免费观看 | 亚洲免费一区二区 | 麻豆成人在线视频 | 中文字幕av第一页 | 亚洲精品国产一区 | 成人av播放 | 午夜电影一区 | 国产精品久久久久久中文字 | 在线观看视频h | 黄色毛片视频 | 亚洲视频在线一区 | a级黄色片视频 | 亚州精品成人 | 国产精品成人一区二区 | 国产jizz女人多喷水99 | h视频在线看 | 久久国产精品免费一区二区三区 | 成人毛片视频在线播放 | 中文字幕亚洲一区二区三区 |