简体   繁体   English

xml解析器数据到文本文件

[英]xml parser data to a text file

i am going through the code for xml parser, and i found this code, but i want to save result data to a text file. 我正在遍历xml解析器的代码,并且找到了此代码,但是我想将结果数据保存到文本文件中。

public class ReadXMLFile 
{

public static void main(String argv[]) 
{

  try {

    File fXmlFile = new File("test.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("test");
    System.out.println("-----------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

       Node nNode = nList.item(temp);
       if (nNode.getNodeType() == Node.ELEMENT_NODE) {

          Element eElement = (Element) nNode;

          System.out.println("ID : " + getTagValue("id", eElement));
          System.out.println("Name : " + getTagValue("Name", eElement));
          System.out.println("AGE: " + getTagValue("age", eElement));

       }
    }
  } 
  catch (Exception e) 
  {
    e.printStackTrace();
  }
 }

 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();
}

}

how can i save the output to a text file ? 如何将输出保存到文本文件?

please suggest me regarding this....... 请给我建议一下.......

Create a Writer to write to a file and replace System.out.println() with Writer.write(). 创建一个Writer写入文件,然后用Writer.write()替换System.out.println()。

BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(s);
writer.newLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM