簡體   English   中英

使用DOM解析器時獲取XML子節點

[英]Get XML child nodes when using DOM parser

我在Java使用DOM解析器,並試圖返回子節點元素。 這是我的XML的樣子:

<?xml version="1.0" encoding="UTF-8"?>
<body>
  <group name="Recently Added">
    <item>
      <asset>138682</asset>
      <date>Mar 05, 2015</date>
      <artist>
        <![CDATA[Marlin G. Meharry, D.D.S]]>
      </artist>
      <album>
        <![CDATA[Career Day - Marlin G. Meharry, D.D.S]]>
      </album>
      <url>
        <![CDATA[http://web2.puc.edu/Departments/Media_Services/file/2015/CarDayMarlinGMeharryDDS.mp3]]>
      </url>
      <length>
        <![CDATA[36:48]]>
      </length>
    </item>
    <item>
      <asset>140705</asset>
      <date>Jan 01, 2000</date>
      <artist>
        <![CDATA[John Nunes]]>
      </artist>
      <album>
        <![CDATA[Educator of the Year]]>
      </album>
      <url>
        <![CDATA[http://web2.puc.edu/Departments/Media_Services/file/2015/2015-4-2LloydBest.mp3]]>
      </url>
      <length>
        <![CDATA[40:46]]>
      </length>
    </item>
  </group>
  <group name="Heubach Lectureship Series">
    <item>
      <asset>48041</asset>
      <date>Mar 07, 2009</date>
      <artist>
        <![CDATA[Barry C. Black, United States Senate Chaplain]]>
      </artist>
      <album>
        <![CDATA[Heubach Lecture]]>
      </album>
      <url>
        <![CDATA[http://web2.puc.edu/Departments/Media_Services/file/2009/HeubachLecture030709.mp3]]>
      </url>
      <length>
        <![CDATA[1:12:57]]>
      </length>
    </item>
  </group>
</body>

因此,在我的根元素下方,我有類似於數組的組節點,因為它們包含項節點。 我需要到達項目節點以獲取其數據。 到目前為止,這是我想出的,但是仍然不確定下一步該怎么做:

URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(in);

NodeList categoryList = doc.getElementsByTagName("group");
for (int categoryNo=0; categoryNo<categoryList.getLength(); categoryNo++) {

    //Element categoryNode = (Element)categoryList.item(categoryNo);

    Node node = categoryList.item(categoryNo);
    NodeList childNodes = categoryList.item(categoryNo).getChildNodes();
    for (int childNo = 0; childNo < childNodes.getLength(); childNo++) {

        Node childNode = childNodes.item(childNo);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            Element elem = (Element) childNode;

            Log.d(elem.getNodeValue(), "Zed");
        }


    }

}

關於如何獲得資產,藝術家,專輯等項目節點的任何提示?

由於elem對應於XML中的item元素,因此可以使用Element#getElementsByTagName(String elementName)檢索子元素,類似於獲取group元素的操作。 然后,您可以使用Node#getTextContent()檢索元素的內容。 例如:

Element elem = (Element) childNode;
NodeList assetNodes = elem.getElementsByTagName("asset");
if(assetNodes.getLength() > 0) {
    // get the text content of the first node
    String asset = assetNodes.item(0).getTextContent();
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM