简体   繁体   English

使用xpath读取后如何将xml值写入csv文件

[英]How to write xml values to csv file after reading using xpath

I am working on a xml file whose values I have to write in a csv file as a output, I am able to read the xml file but I am not able to produce the csv file using that values, I am giving the xml and the java code what I have done so far, 我正在处理一个xml文件,我必须将其值写到一个csv文件中作为输出,我能够读取该xml文件,但是我无法使用该值生成csv文件,我给了xml和Java代码,到目前为止我所做的,

<?xml version="1.0" ?>
<ThirdParty>
 <Name>Karen Wallace</Name> 
 <Phone>785-296-7829</Phone> 
<State>KS</State> 
<Address1>420 SW 9th Street</Address1> 
 <pxObjClass>PCore-Compliance-Commsys-Data-ThirdParty</pxObjClass> 
<Zip>66612</Zip> 
<City>Topeka</City> 
 </ThirdParty>

This is my xml file whose value I am able to print on the console, I need to write it to a csv file , where each tag will be there header and the values will be the values within the tag. 这是我的xml文件,我可以在控制台上打印该文件的值,我需要将其写入csv文件,其中每个标签都将包含标头,而值将是标签内的值。

public class Main {

public static void main(String[] args) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {

  Writer writer = new OutputStreamWriter(new FileOutputStream("src/main/resources/pathToFile"), "UTF-8");
  BufferedWriter bw = new BufferedWriter(writer);   
  DocumentBuilderFactory domFactory = 
  DocumentBuilderFactory.newInstance();
  domFactory.setNamespaceAware(true); 
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse("src/main/resources/new.xml");
  XPath xpath = XPathFactory.newInstance().newXPath();
   // XPath Query for showing all nodes value

      XPathExpression expr = xpath.compile("pagedata/ThirdParty/*/text()");
      String pyTemporaryObject = null ,line = null;
  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeList) result;
  for (int i = 0; i < nodes.getLength(); i++) {

  System.out.println(nodes.item(i).getNodeValue()); // this line prints the output.

   // this piece of code I have tried to write into a csv which did not worked

          Node node = nodes.item(i);                                
          Element element = (Element) node;
          pyTemporaryObject = element.getElementsByTagName("pyTemporaryObject").item(0).getTextContent();
          line  = String.format("%s", pyTemporaryObject);
          System.out.println(pyTemporaryObject);
          bw.write(line);
          bw.newLine();
          bw.flush();Node node = nodes.item(i);


          Element element = (Element) node;
          pyTemporaryObject = element.getElementsByTagName("pyTemporaryObject").item(0).getTextContent();
          line  = String.format("%s", pyTemporaryObject);
          System.out.println(pyTemporaryObject);
          bw.write(line);
          bw.newLine();
          bw.flush();



   }
     }
 } 

Please help me to produce the csv file whose structure is 请帮助我产生结构为

Name   |   Phone    |    State  |   Address1  |       pxObjClass  |             Zip  | City

Karen Wallace | 785-296-7829 | KS |  420 SW 9th Street | PCore-Compliance-Commsys-Data-ThirdParty | 66612 | Topeka

Each tag will be the header column and the value will be there below it. 每个标签将是标题列,而值将在其下方。

CSV stands for Comma Separated Values so you need to include comma separator in your line to write so it would look something like this (assuming you do all resource desposal, node checking etc): CSV代表逗号分隔值,因此您需要在行中包含逗号分隔符才能编写,因此它看起来像这样(假设您进行了所有资源处理,节点检查等):

bw.write("Name,Phone,State,Address1,pxObjClass,Zip,City");

for (int i = 0; i < nodes.getLength(); i++) {
{
    Node node = nodes.item(i);                                
    Element element = (Element) node;
    String name = element.getElementsByTagName("Name").item(0).getTextContent();
    String phone = element.getElementsByTagName("Phone").item(0).getTextContent();
    String state = element.getElementsByTagName("State").item(0).getTextContent();
    String address1 = element.getElementsByTagName("Address1").item(0).getTextContent();
    String pxObjClass = element.getElementsByTagName("pxObjClass").item(0).getTextContent();
    String zip = element.getElementsByTagName("Zip").item(0).getTextContent();
    String city = element.getElementsByTagName("City").item(0).getTextContent();

line  = String.format("%s,%s,%s,%s,%s,%s,%s", name,phone,state,address1,pxObjClass,zip,city);
bw.write(line);
bw.newLine();
bw.flush();
}

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

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