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

时间:2017-04-27 14:44:00

标签: java xml csv xpath

我正在处理一个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>

这是我的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();



   }
     }
 } 

请帮我生成结构为

的csv文件
Name   |   Phone    |    State  |   Address1  |       pxObjClass  |             Zip  | City

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

每个标记都是标题列,值将在其下方。

1 个答案:

答案 0 :(得分:0)

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