2011-10-26 221 views
1

我想解析以下網址:http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=nucleotide&id=224589801解析XML文件

當我想出了下面的方法結果:

public void parseXml2(String URL) { 
    DOMParser parser = new DOMParser(); 

    try { 
     parser.parse(new InputSource(new URL(URL).openStream())); 
     Document doc = parser.getDocument(); 

     NodeList nodeList = doc.getElementsByTagName("Item"); 
     for (int i = 0; i < nodeList.getLength(); i++) { 
      Node n = nodeList.item(i); 
      Node actualNode = n.getFirstChild(); 
      if (actualNode != null) { 
       System.out.println(actualNode.getNodeValue()); 
      } 
     } 

    } catch (SAXException ex) { 
     Logger.getLogger(TaxMapperXml.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IOException ex) { 
     Logger.getLogger(TaxMapperXml.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

用這種方法我可以取值Item節點,但我不能接受它們的任何屬性。我試着用NamedNodeMap試驗getAttribute(),但仍無濟於事。

  1. 爲什麼我要做n.getFirstChild().getNodeValue();得到的實際價值? n.getNodeValue()只返回null?這不是反直覺 - 顯然在我的情況下,節點的沒有子節點?

  2. 是否有一些更強大和更廣泛接受的使用DOM解析XML文件的方式?我的文件都不會是最多大15-20行,所以SAX是沒有必要的(是嗎?)

+0

您可以編寫一個簡單的助手類來完成圍繞DOM的任務。看到這個http://stackoverflow.com/a/8346867/851432 – Jomoos

回答

5
import java.io.IOException; 
import java.net.URL; 
import org.apache.xerces.parsers.DOMParser; 

import org.w3c.dom.Document; 
import org.w3c.dom.NamedNodeMap; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 

public class XMLParser { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     parseXml2("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=nucleotide&id=224589801"); 
    } 

    public static void parseXml2(String URL) { 
     DOMParser parser = new DOMParser(); 

     try { 
      parser.parse(new InputSource(new URL(URL).openStream())); 
      Document doc = parser.getDocument(); 

      NodeList nodeList = doc.getElementsByTagName("Item"); 
      for (int i = 0; i < nodeList.getLength(); i++) { 
       System.out.print("Item "+(i+1)); 
       Node n = nodeList.item(i); 
       NamedNodeMap m = n.getAttributes(); 
       System.out.print(" Name: "+m.getNamedItem("Name").getTextContent()); 
       System.out.print(" Type: "+m.getNamedItem("Type").getTextContent()); 
       Node actualNode = n.getFirstChild(); 
       if (actualNode != null) { 
        System.out.println(" "+actualNode.getNodeValue()); 
       } else { 
        System.out.println(" ");      
       } 
      } 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

完成了示例代碼,並添加了幾行,以獲得屬性。

這應該讓你開始,雖然我覺得你需要讓自己瞭解DOM的基本概念。 This網站(和許多其他人)可以幫助你。最重要的是理解不同種類的節點。

6
  1. 由XML標記包圍的文本值也被認爲是DOM中的節點。這就是爲什麼你必須在獲得價值之前獲得文本節點。如果您嘗試對<Item>中的節點數進行計數,您會看到只要有文本,就有一個節點。

  2. XOM具有更直觀的界面,但它沒有org.w3c.dom.*界面。

如果你想使用內置的解析器,你應該看看http://www.java-samples.com/showtutorial.php?tutorialid=152

DOMParser您嘗試使用的正當性,它不便於攜帶。

+0

是啊,或者jdom ... –

1

xml元素中的文本在文本節點中,因爲子元素可以與文本混合使用。例如:

... 
<A>blah<B/>blah</A> 
... 

元素A有三個子元素:文本節點,元素B,另一個文本節點。