簡體   English   中英

用Java讀取XML文件

[英]XML file reading in Java

在用Java讀取XML文件之前,是否需要完全了解XML文件的結構和標簽?

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

在讀取文件之前,我不知道字段名稱“已檢查”。 有什么辦法可以列出XML文件中的所有標簽,基本上是文件結構嗎?

我自己編寫了此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);  

            }         

     }  

}

您絕對應該為此檢查出庫,例如dom4j( http://dom4j.sourceforge.net/ )。 他們可以解析整個XML文檔,不僅讓您列出諸如元素之類的內容,而且還可以對它們進行XPath查詢和其他類似的工作。

這會對性能產生影響,特別是在大型XML文檔中,因此您需要在提交庫之前檢查用例的性能影響。 如果您只需要一點點XML文檔(並且您已經知道要查找的內容),則尤其如此。

您問題的答案是否定的,沒有必要事先知道任何元素名稱。 例如,您可以在樹上走動以發現元素名稱。 但這一切都取決於您實際上要做什么。

順便說一下,對於絕大多數應用程序,Java DOM是解決問題的最糟糕的方法之一。 但是在不了解您的項目需求的情況下,我不會進一步發表評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM