2013-09-26 68 views
0

我試圖用java解析維基百科XML頁面,如http://en.wikipedia.org/w/api.php?action=query&prop=revisions&format=xml&rvlimit=10&titles=test解析維基百科XML和Java

我最感興趣的是拍攝

<normalized> 
    <n from="test" to="Test" /> 
</normalized> 

這裏所示的標題我的代碼已經建立連接了,我只是停留在捕獲標題Test的獲取和設置部分。我發現的所有教程似乎都是針對計算機上的xml文件的,但沒有一個顯示在線捕捉並解析它。

回答

0

這個有什麼問題?

解析(InputStream爲)
將給定InputStream的內容作爲一個XML文檔,並且返回一個新的DOM文檔

使用的getInputStream()您的URLConnection實例的方法,並用它來構建XML文件

0

我發現的所有教程似乎都是針對計算機上的xml文件的,但沒有一個顯示在線捕捉並解析它。 //

解析XML文件網上不從本地解析一個

0

不同,您需要使用DocumentBuilderFactory

下面是關於如何使用一個簡單的例子就

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 
Document doc = docBuilder.parse(new InputSource(new URL(URL).openStream())); 

NodeList nodeList = doc.getElementsByTagName("elementnamehere"); 

for (int temp = 0; temp < nodeList.getLength(); temp++) { 

     Node nNode = nodeList.item(temp); 
     Element eElement = (Element) nNode; 
     if(eElement.getAttribute("elementnamehere") != null) 
     { 
      //Do something with it 

     } 
     else 
     { 

     } 

    }