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

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

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

問題描述

我的問題是:當存在與父元素的孫子"同名的其他元素時,如何直接獲取特定父元素下的元素.

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 庫 解析 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>

如您所見,您可以在兩個地方放置 <file> 元素.無論是在組內(nèi)還是在組外.我真的希望它采用這種結(jié)構(gòu),因為它對用戶更友好.

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)在,每當我調(diào)用 notificationElement.getElementsByTagName("file"); 時,它都會為我提供所有 <file> 元素,包括 < 下的元素;group> 元素.我以不同的方式處理這些文件中的每一種,所以這個功能是不可取的.

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. 獲取文件元素的父元素并進行相應(yīng)處理(取決于是還是.
  2. 重命名第二個 <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)于最佳"方法的評論和回答,但我對 DOM 解決方案真的很感興趣,因為這就是其余的這個項目正在使用.謝謝.

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.

推薦答案

嗯,這個問題的 DOM 解決方案其實很簡單,雖然不太優(yōu)雅.

Well, the DOM solution to this question is actually pretty simple, even if it's not too elegant.

當我遍歷調(diào)用notificationElement.getElementsByTagName("file")時返回的filesNodeList時,我只檢查父節(jié)點的名稱是否為通知"".如果不是,那么我將忽略它,因為這將由 <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 直接子元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進度偵聽器未觸發(fā)(Google 驅(qū)動器 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 - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅(qū)動api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 欧美自拍日韩 | 久草青青草 | 精品一区二区三区中文字幕 | 成人免费视频网 | 久久久久久久一区二区三区 | 偷拍自拍网站 | 欧美亚洲国产精品 | av中文在线播放 | 日韩精品无码一区二区三区 | 免费看欧美一级片 | 99精品欧美一区二区三区 | 国产在线观看网站 | 久久久成人网 | jizz亚洲人| 欧美一级特黄aaa大片在线观看 | 国产三区视频在线观看 | 日韩欧美中文字幕在线观看 | 91毛片网| 91精品国产91久久久久游泳池 | 欧美电影网 | 久草视频在线播放 | 欧美一卡二卡在线 | 精品久久久久久久久亚洲 | 日韩一区二区三区四区五区六区 | 人人种亚洲 | 91精品91久久久| 91久久精品国产 | 中文字幕一区二区三区四区 | 欧美中文字幕 | 日本一区二区三区精品视频 | 亚洲免费精品 | 日韩在线观看一区 | 欧美日韩精品一区二区三区视频 | 亚洲一区二区三区四区五区中文 | 日韩中文字幕 | 亚洲欧美综合精品久久成人 | 女女百合av大片一区二区三区九县 | 人人干在线| 日韩欧美在线一区二区 | 日本精品一区二区三区在线观看视频 | 国产精品久久网 |