2012-06-20 144 views
-5
 I Want To Parse XML simply using any Parsing Method ,I Have a XML with following format, Please give me help urgently, 

波纹管是我使用DOM解析器,但我没有得到继承XML我试过的样品,,如何使用多节点解析XML?

<Main> 
     <name> 
      <firstname>ABC</firstname> 
      <lastname>XYZ</lastname> 
    </name> 
    <address> 
     <homeaddress> 
       <pobox>PQR</pobox> 
       <city>MNQ</city> 
     </homeaddress> 
     <officeaddress>  
       <pobox>JKL</pobox> 
       <city>URI</city> 
      </officeaddress> 
    </address> 
    <Main> 
+6

我们来这里不是为你工作(除非你付钱给我们......)。然后,当出现具体问题时,请做出努力,然后正确地提出问题。 – Th0rndike

+0

谢谢Thorndike给我免费的建议.. – Avinash

+0

@Avinash首先打谷歌,如果它没有被阻止你。这个链接可能会有帮助.http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/ – vrs

回答

1

我显示你,我已经解析我的数据的方式...希望你可以用 实现你的。

allClientParser.java

import java.io.IOException; 
import java.io.StringReader; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

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

public class allClientParser { 

    public Client parseByDOM(String response) 
      throws ParserConfigurationException, SAXException, IOException { 

     Client c = new Client(); 

     try { 

      DocumentBuilderFactory dbFactory = DocumentBuilderFactory 
        .newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 

      Document doc = dBuilder.parse(new InputSource(new StringReader(
        response))); 
      doc.getDocumentElement().normalize(); 
      NodeList nList = doc.getElementsByTagName("row"); 

      c.length = nList.getLength(); 
      for (int temp = 0; temp < nList.getLength(); temp++) { 
       Node nNode = nList.item(temp); 

       if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
        Element eElement = (Element) nNode; 
        c.clientdata[temp][1] = getTagValue("client_id", eElement); 
        c.clientdata[temp][2] = getTagValue("name", eElement); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return c; 
    } 

    private static String getTagValue(String sTag, Element eElement) { 
     NodeList nlList = eElement.getElementsByTagName(sTag).item(0) 
       .getChildNodes(); 

     Node nValue = (Node) nlList.item(0); 

     return nValue.getNodeValue(); 
    } 
} 

Client.java

public class Client { 

    public int length; 
    public String clientdata[][] = new String[500][500]; 
} 

现在从我们就必须通过你的XML像这样的字符串来调用它的主要方法::

Client abc; 
allClientParser cl=new allClientParser(); 
abc=cl.parseByDOM(pass your xml as String here); 

现在,您可以访问您的数据这样的:

String name=abc.clientdata[x][y]; 
+1

好的... @ JoxTraex ...但在我看来,这里的提问者是一个新手到XML解析..因此,他很容易undersatand .. :( –