首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java读取XML文件

问用Java读取XML文件
EN

Stack Overflow用户
提问于 2014-01-08 04:53:05
回答 3查看 162关注 0票数 0

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

代码语言:javascript
复制
areaElement.getElementsByTagName("checked").item(0).getTextContent()

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-08 07:58:11

我自己准备了这个DOM解析器,使用递归来解析xml,而不了解单个标记。如果存在,它将按顺序给出每个节点的文本内容。您可以删除以下代码中的注释部分,以获得节点名称。希望能帮上忙。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-01-08 05:00:01

为此,您肯定应该查看库,比如dom4j (http://dom4j.sourceforge.net/)。它们可以解析整个XML文档,并允许您不仅列出元素之类的内容,还可以在它们上执行XPath查询和其他类似的很酷的操作。

性能会受到影响,特别是在大型XML文档中,因此在提交库之前,您需要检查性能受损的用例。如果您只需要从XML文档中提取一点点内容(而且您已经知道要寻找的是什么),这一点尤其正确。

票数 0
EN

Stack Overflow用户

发布于 2014-01-08 09:05:20

你的问题的答案是否定的,没有必要事先知道任何元素的名称。例如,您可以遍历树来发现元素名。但这完全取决于你到底想做什么。

顺便说一句,对于绝大多数应用程序来说,Java是解决问题的最糟糕的方法之一。但是,在不了解您的项目需求之前,我不会进一步评论。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20987309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档