Frage

I'm extremely new to Java and have been following this tutorial to convert a CSV file to XML in Mule ESB, using a Java class that makes use of Flatpack. Here is the input file I'm using:

Item1|Item2|Item3|Item4
Item5|Item6|Item7|Item8

And here is my desired output:

<root>
    <rows>
        <a>Item1</a>
        <b>Item2</b>
        <c>Item3</c>
        <d>Item4</d>
    </rows>
    <rows>
        <a>Item5</a>
        <b>Item6</b>
        <c>Item7</c>
        <d>Item8</d>
    </rows>
</root>

I've used the Java script exactly as it's given in the tutorial:

public class CsvConverter {

 private static char delimiter = '|';
 private static char qualifier = '"';
 private static boolean ignoreFirstRecord = false;

 public CsvConverter() {

 }

 public String convert(String csv)  {
  StringReader sr = new StringReader(csv);
  FileReader fr = null;

  //Get the configuration file for csv processing
  try {
   fr = new FileReader("src/main/resources/usermap.xml");
  } catch (FileNotFoundException e) {   
   e.printStackTrace();
  }

  //The csv parser
  Parser p = DefaultParserFactory.getInstance().newDelimitedParser(fr, sr,delimiter, qualifier, ignoreFirstRecord);

  DefaultDataSet d = (DefaultDataSet) p.parse();

  //Build the xml
  DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
        DocumentBuilder build;
        Document doc=null;
  try {
   build = dFact.newDocumentBuilder();

        doc = build.newDocument();
  } catch (ParserConfigurationException e) {

   e.printStackTrace();
  }
        Element root = doc.createElement("root");
        doc.appendChild(root);

        Element Details = doc.createElement("rows");
        root.appendChild(Details);


  while (d.next()) {

    String[] colums = d.getColumns();
    for(int i=0; i<colums.length;i++ ){
     Element name = doc.createElement(colums[i]);
              name.appendChild(doc.createTextNode(d.getString(colums[i])));
              Details.appendChild(name);
    }

  }
  TransformerFactory tf = TransformerFactory.newInstance();
  Transformer transformer=null;
  StringWriter writer = null;

  try {
   transformer = tf.newTransformer();
   writer = new StringWriter();
   transformer.transform(new DOMSource(doc), new StreamResult(writer));
  } catch (TransformerConfigurationException e) {

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

   e.printStackTrace();
  }


  String output = writer.getBuffer().toString();

  return output;

 }

}

And this is the usermap.xml file:

<?xml version="1.0"?>
<!DOCTYPE PZMAP SYSTEM "flatpack.dtd">
<PZMAP>
<COLUMN name="a"/>
<COLUMN name="b"/>
<COLUMN name="c"/>
<COLUMN name="d"/>
</PZMAP>

Now, the output I'm actually getting looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <rows>
        <a>Item1</a>
        <b>Item2</b>
        <c>Item3</c>
        <d>Item4</d>
        <a>Item5</a>
        <b>Item6</b>
        <c>Item7</c>
        <d>Item8</d>
    </rows>
</root>

As you can see it's not placing each row in it's own <rows> tag, instead it is combining all rows into one <rows> tag. I have a very limited knowledge of Java and have so far been unable to diagnose the problem. Is it possible to modify the Java script I'm using here to create the desired output I've outlined above? Thanks in advance.

UPDATE

Following the answer provided by @TheManzet, with a little experimentation the following modification to the while loop worked:

while (d.next()) {

    Element Details = doc.createElement("rows");

    String[] colums = d.getColumns();
    for(int i=0; i<colums.length;i++ ){
     Element name = doc.createElement(colums[i]);
              name.appendChild(doc.createTextNode(d.getString(colums[i])));
              Details.appendChild(name);
    }

    root.appendChild(Details);

  }
War es hilfreich?

Lösung

I don't have a way to test at the moment, but I would try moving root.appendChild(Details); inside of your while loop immediately after while (d.next()) {

Right now you only append a rows element once, if you do it for each row, it should work.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top