【问题标题】:Reading XML data into text file将 XML 数据读入文本文件
【发布时间】:2012-08-03 06:03:58
【问题描述】:

我是编码新手,谁能帮我纠正我的代码。 我需要读取 XML 数据并将数据存储在文本文件中。每个值必须用逗号分隔,一旦读取一条记录的数据,就需要转到下一行并执行另一条记录。

示例 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
 <xml_tool xmlns:md="http://www.example.com/XT/1.0/">  
    <md:header>       
          <md:application_version>1.0</md:application_version>      
         <md:export_date>19-04-2012</md:export_date>   
          <md:export_time>14:55</md:export_time>   
          <md:export_user>USER01</md:export_user> 
    </md:header>   
    <md:table table_name="CUSTOMER" key="customer number" record_count="2" column_count="5">  
          <md:record>   
                                        <md:column name="customer_number">123456</md:column>   
                                         <md:column name="reg_date">01-04-2012</md:column>   
                                          <md:column name="customer_name">Test Customer</md:column>  
                                          <md:column name="customer_type">Normal </md:column> 
                                          <md:column name="comments">This is a test record</md:column>       
            </md:record>     
             <md:record>             
                                           <md:column name="customer_number">555111</md:column>   
                                            <md:column name="reg_date">02-04-2012</md:column>        
                                            <md:column name="customer_name">Test Customer</md:column>     
                                            <md:column name="customer_type"> </md:column>            
                                             <md:column name="comments">This is a test record</md:column>         
                </md:record>     
      </md:table>
</xml_tool

我的代码:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.*;

public class ParseXML {

    public static void main(String argv[])throws IOException {

         try {     
             BufferedWriter writer = new BufferedWriter(new FileWriter("results/staff.txt")); 
             File fXmlFile = new File("data/hardware-info.xml");  

             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();  
             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();      
             Document doc = dBuilder.parse(fXmlFile);      
             doc.getDocumentElement().normalize();    
             NodeList nList = doc.getElementsByTagName("md:record");     
             for (int i = 0; i < nList.getLength(); i++) 
             {      
                 Node node = nList.item(i);    
                 if (node.getNodeType() == Node.ELEMENT_NODE)    
                 {         
                      Element eElement = (Element) node; 
                     if(eElement.hasChildNodes())    
                     {               
                         NodeList nl = node.getChildNodes();      
                         for(int j=0; j<nl.getLength(); j++)  
                         {                   
                             Node nd = nl.item(j);   
                             String name= nd.getTextContent();
                                f (name != null && !name.trim().equals(""))                                 {
                                    System.out.print(name.trim()+",");   
                                    //System.out.print(" ");
                                    writer.write(nd.getTextContent().trim() + " ");
                                }

                         } 
                         System.out.println("");
                         writer.write("\n");
                         }        
                         } }  
             writer.close();
             } 
         catch (Exception e) {       
                         e.printStackTrace();     } } 
    @SuppressWarnings("unused")
    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();     
        } 
}

此代码的输出:

123456,01-04-2012,Test Customer,Normal,,This is a test record,
555111,02-04-2012,Test Customer,This is a test record,

预期输出:

123456,01-04-2012,Test Customer,Normal,This is a test record
555111,02-04-2012,Test Customer, ,This is a test record

【问题讨论】:

    标签: java xml-parsing domparser


    【解决方案1】:

    我怀疑这是你的问题:

    String name = nd.getTextContent();
    if (name != null)
    

    您可能想要:

    String name = nd.getTextContent();
    if (name != null && name.length() > 0)
    

    【讨论】:

      【解决方案2】:

      if (name != null) 更改为

      if (name != null &amp;&amp; !name.trim().equals(""))

      【讨论】:

      • 123456,01-04-2012,Test Customer,Normal,这是一个测试记录, 555111,02-04-2012,Test Customer,VIP,这是一个测试记录,我得到了以这种方式输出最后一个值读取数据时不应该执行","
      • 当它读取没有数据的空标签时,它需要写入空白空间,如果 (name != null && !name.trim().equals("")) 我完全删除一个标签
      • 然后将您的 for 循环条件更改为 nList.getLength()-1 并在 for 循环之外手动附加第 n 条记录(您可能希望将整个块移动到一个方法并在 for 内部和外部调用该方法-loop) 或简单地覆盖文件中的最后一个 , 。
      • 使用RandomAccessFile(查看javadocs docs.oracle.com/javase/6/docs/api/java/io/…)或修改for循环写入n-1条记录,并在for循环外写入第n条记录
      猜你喜欢
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2013-03-11
      相关资源
      最近更新 更多