用Java读取XML文件

时间:2014-01-08 04:53:05

标签: java xml dom

在用Java读取之前是否有必要完全了解XML文件的结构和标签?

areaElement.getElementsByTagName("checked").item(0).getTextContent()

在我读取文件之前,我不知道字段名称是“已检查”。有没有办法列出XML文件中的所有标签,基本上是文件结构?

3 个答案:

答案 0 :(得分:1)

我自己编写了这个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);  

            }         

     }  

}

答案 1 :(得分:0)

你应该检查一下这个库,比如dom4j(http://dom4j.sourceforge.net/)。他们可以解析整个XML文档,不仅可以列出元素之类的内容,还可以查看XPath查询和其他类似的东西。

性能受到影响,特别是在大型XML文档中,因此您需要在提交到库之前检查用例的性能影响。如果您只需要XML文档中的一小部分(并且您已经知道您正在寻找的内容),则尤其如此。

答案 2 :(得分:0)

您的问题的答案是否定的,没有必要提前知道任何元素名称。例如,您可以遍历树以发现元素名称。但这一切都取决于你实际上要做的事情。

对于绝大多数应用程序而言,Java DOM是解决问题的最糟糕方法之一。但如果不了解您的项目要求,我不会进一步评论。