简体   繁体   中英

How can we convert XML file to CSV using Java Code?

I want to convert XML file to CSV using Java Code, i don't want to use XML Stylesheet(XSL) or XSLT. Here is my XML file.

<?xml version="1.0" encoding="UTF-8"?>
<PickAndPlace>
<Components>
    <Component id="1">
        <X_Dimension>4.33</X_Dimension>
        <Y_Dimension>2.962</Y_Dimension>
        <Designation>None</Designation>
        <Package>None</Package>
        <Angle>0</Angle>
    </Component>
    <Component id="5">
        <X_Dimension>4.33</X_Dimension>
        <Y_Dimension>8.692</Y_Dimension>
        <Designation>None</Designation>
        <Package>None</Package>
        <Angle>0</Angle>
    </Component>
    <Component id="9">
        <X_Dimension>4.33</X_Dimension>
        <Y_Dimension>14.381</Y_Dimension>
        <Designation>None</Designation>
        <Package>None</Package>
        <Angle>0</Angle>
    </Component>
</Components>
</PickAndPlace>

Here what i want as my CSV Output.

X_Dimension,Y_Dimension,Designation,Package,Angle,_id
4.33,2.962,None,None,0,1
4.33,8.692,None,None,0,5
4.33,14.381,None,None,0,9

You could read the file line-by-line, extracting only the data you need, and storing everything into a temporary LinkedList of Strings:

    LinkedList<String> tmpList = new LinkedList<String>();
    try (
        BufferedReader reader = Files.newBufferedReader(Paths.get("c:/tmp.xml"), Charset.forName("UTF-8"))) {
        String line = StringUtils.EMPTY;
        while ((line = reader.readLine()) != null) {
            if(line.contains("<Component id=")) {
                String _id = extractValue(line, "<Component id=\"", "\">");
                String _xDimension = extractValue(reader.readLine(), "<X_Dimension>", "</X_Dimension>");
                String _yDimension = extractValue(reader.readLine(), "<Y_Dimension>", "</Y_Dimension>");
                String _designation = extractValue(reader.readLine(), "<Designation>", "</Designation>");
                String _package = extractValue(reader.readLine(), "<Package>", "</Package>");
                String _angle = extractValue(reader.readLine(), "<Angle>", "</Angle>");
                tmpList.add(_xDimension + "," + _yDimension + "," + _designation + "," + _package + "," + _angle + "," + _id);
            }

        }
    } catch (IOException e) {
        System.err.println(e);
    }

This handy utility method will deal with extracting values for the above code. Note that it may need to be made more robust depending on your data and requirements, but it works fine for the sample-set you provided:

private static String extractValue(String line, String prefix, String postfix) {
    String value = line.trim().replaceAll(prefix, "");
    value = value.replaceAll(postfix, "");
    return value;
}

Once read, you could write the LinkedList of Strings to a new file:

    try{
        PrintWriter writer = new PrintWriter("c:/tmp.csv", "UTF-8");
        writer.println("X_Dimension,Y_Dimension,Designation,Package,Angle,_id");
        for(String line : tmpList) {
            writer.println(line);
        }
        writer.close();
    } catch (IOException e) {
        System.err.println(e);
    }

Of course, this method relies heavily on the XML data being consistently structured like this throughout.

As a final note, you could remove the need for the temporary list by writing out to a file directly, instead of adding values to a list first. It is nice to separate input and output in code though.

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