2014-01-29 98 views
0
private void dislpay() { 
     try { 
      File fXmlFile = new File("/data/data/com.example.addnode/Add.xml"); 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(fXmlFile); 
      doc.getDocumentElement().normalize(); 
      System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); 
      NodeList nList = doc.getElementsByTagName("details"); 
      for (int temp = 0; temp <= nList.getLength(); temp++) { 
       Node nNode = nList.item(temp); 
       if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
        Element eElement = (Element) nNode; 

        String a=eElement.getElementsByTagName("firstname").item(temp).getTextContent().toString(); 
        String b=eElement.getElementsByTagName("lastname").item(temp).getTextContent().toString(); 
        String c=eElement.getElementsByTagName("nickname").item(temp).getTextContent().toString(); 

        System.out.println(a); 
        System.out.println(b); 
        System.out.println(c); 
       } 
      } 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } 

XML是检索子节点值

<?xml version="1.0" encoding="UTF-8"?> 
    <user> 
     <details id="1"> 
     <firstname>JOHN</firstname> 
     <lastname>R</lastname> 
     <nickname>JJ</nickname> 
     </details> 
     <details> 
     <firstname>NOMAN</firstname> 
     <lastname>K</lastname> 
     <nickname>NK</nickname> 
     </details>  
    </user> 

预期输出:

JOHN R JJ 
NOMAN K NK 

电流输出是:

JOHN R JJ 

我想显示的子节点的所有值(细节),但是当我执行应用程序时,它只显示第一个t值得的不是全部。我正在学习XML,因此我对XML没有太多的了解。所以请指导我。

回答

0

在你现有的代码,改正你的错误......,

//Remove = otherwise loop will run 3 times and you will get null pointer exception 
for (int temp = 0; temp < nList.getLength(); temp++) 

// Replace temp with 0 here because temp is increasing with your for loop and this is first node of this element always 

       String a=eElement.getElementsByTagName("firstname").item(0).getTextContent().toString(); 
       String b=eElement.getElementsByTagName("lastname").item(0).getTextContent().toString(); 
       String c=eElement.getElementsByTagName("nickname").item(0).getTextContent().toString(); 

为了您的方便, 我已经准备通过自己这个DOM解析器,使用递归将分析XML,而无需单个知识标签。它将为您提供每个节点的文本内容(如果存在),按顺序排列。您可以删除以下代码中的注释部分以获取节点名称。希望它会有所帮助。

import java.io.BufferedWriter; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.OutputStreamWriter; 

    import javax.xml.parsers.DocumentBuilder; 
    import javax.xml.parsers.DocumentBuilderFactory; 
    import org.w3c.dom.Document; 
    import org.w3c.dom.Node; 
    import org.w3c.dom.NodeList; 



    public class RecDOMP { 


    public static void main(String[] args) throws Exception{ 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      dbf.setValidating(false); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 

    // replace following path with your input xml path 
      Document doc = db.parse(new FileInputStream(new File ("D:\\ambuj\\ATT\\apip\\APIP_New.xml"))); 

    // replace following path with your output xml path 
      File OutputDOM = new File("D:\\ambuj\\ATT\\apip\\outapip1.txt"); 
       FileOutputStream fostream = new FileOutputStream(OutputDOM); 
       OutputStreamWriter oswriter = new OutputStreamWriter (fostream); 
       BufferedWriter bwriter = new BufferedWriter(oswriter); 

       // if file doesnt exists, then create it 
       if (!OutputDOM.exists()) { 
        OutputDOM.createNewFile();} 


       visitRecursively(doc,bwriter); 
       bwriter.close(); oswriter.close(); fostream.close(); 

       System.out.println("Done"); 
    } 
    public static void visitRecursively(Node node, BufferedWriter bw) throws IOException{ 

       // get all child nodes 
      NodeList list = node.getChildNodes();         
      for (int i=0; i<list.getLength(); i++) {   
        // get child node    
      Node childNode = list.item(i); 
      if (childNode.getNodeType() == Node.TEXT_NODE) 
      { 
     //System.out.println("Found Node: " + childNode.getNodeName()   
     // + " - with value: " + childNode.getNodeValue()+" Node type:"+childNode.getNodeType()); 

     String nodeValue= childNode.getNodeValue(); 
     nodeValue=nodeValue.replace("\n","").replaceAll("\\s",""); 
     if (!nodeValue.isEmpty()) 
     { 
      System.out.println(nodeValue); 
      bw.write(nodeValue); 
      bw.newLine(); 
     } 
      } 
      visitRecursively(childNode,bw); 

       }   

     } 

    } 
+0

thanku sir它的工作:) –