简体   繁体   中英

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 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.

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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