2011-06-10 55 views
1

它在XML中看起來像這樣。我想他圖片src值...如何解析<img src="value" from this XML by using DOM?

<description><![CDATA[<div class="images"><img src="http://www.voicetv.co.th/cache/images/8a1a6f2aeb7b0e9c1d6bb3eae314165f.jpg" /></div>]]></description> 

我在做什麼是

if ((theElement.getElementsByTagName("description")).getLength() > 0) { 

      allChildern = theElement.getElementsByTagName("description").item(0).getChildNodes(); 

      for (int index = 0; index < allChildern.getLength(); index++) { 
       description += allChildern.item(index).getNodeValue(); 

       NodeList chNodes = allChildern.item(index).getChildNodes(); 
       for (int i = 0; i < chNodes.getLength(); i++) { 

        String name = chNodes.item(i).getNodeName(); 
        if(name.equals("div")) { 
         String clas = allChildern.item(index).getAttributes().getNamedItem("class").getNodeValue(); 
         if(clas.equals("images")){ 
          String nName = allChildern.item(index).getChildNodes().item(0).getNodeName(); 
          if(nName.equals("img")) { 
           String nValue = allChildern.item(index).getChildNodes().item(0).getAttributes().getNamedItem("src").getNodeValue(); 
          } 
         } 
        } 
       } 


      } 
      currentStory.setDescription(description); 
     } 

但就是不工作

回答

5

描述元素包含一個CDATA節點。這意味着您嘗試訪問的<img>「元素」實際上只是一段文本(而不是元素)。

您需要將文本解析爲新的XML文檔才能通過DOM方法訪問它。

+0

我只是得到描述節點的值。放在標記。由於DOM允許1個根元素。在這裏它看起來像 DocumentBuilderFactory fectory = DocumentBuilderFactory.newInstance(); \t \t \t的DocumentBuilder助洗劑= fectory.newDocumentBuilder();的InputStream的inputStream =新ByteArrayInputStream進行((」 「+ currentStory.getDescription()+」 「).getBytes(」 UTF-8" )); Document document = builder.parse(inputStream); \t \t \t String imageURL = document.getElementsByTagName(「img」)。item(0).getAttributes()。getNamedItem(「src」)。getNodeValue(); \t \t \t currentStory.setImagePath(imageURL); – Arslan 2011-06-10 07:36:34

0

警告:這可能有點髒,如果xml可以包含類似於圖片標籤的註釋,它也可能很脆弱。

對於具有cdata部分的簡短xml片段使用xml解析的替代方法是使用regexp獲取圖像url。這裏有一個例子:

String xml = "<description><![CDATA[<div class=\"images\"><img src=\"http://www.voicetv.co.th/cache/images/8a1a6f2aeb7b0e9c1d6bb3eae314165f.jpg\"/></div>]]></description>"; 
Matcher matcher = Pattern.compile("<img src=\"([^\"]+)").matcher(xml); 
while (matcher.find()) { 
    System.out.println("img url: " + matcher.group(1)); 
} 
+0

謝謝你可以選擇。讓我檢查一下。 – Arslan 2011-06-10 06:48:31