mancha89 mancha89 - 6 months ago 57
Java Question

Splitting XML into multiple xml files

I have an XML file that has the data for multiple pictures such as GPS coordinates, date/time, and some image data that I need to split into several XML files.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<Placemark>
<name> //picture 1 info <name>
<Point>
<coordinates> //gps 1 cords <coordinates>
<Point>
<Placemark>
<Placemark>
<name> //picture 2 info <name>
<Point>
<coordinates> //gps 2 cords <coordinates>
<Point>
<Placemark>
<Document>


I want it to look something like this:

File 1:
<Placemark>
<Name> //picture 1 info <name>
<Point>
<coordinates> //gps 1 cords <coordinates>
<Point>
<Placemark>

File 2:
<Placemark>
<Name> //picture 2 info <name>
<Point>
<coordinates> //gps 2 cords <coordinates>
<Point>
<Placemark>


.....I read this question:
Split XML in Multiple XML files

and tried to modify the code a little bit for my file after importing everything. Wondering if anyone had any good ideas on how to modify my code to do the spit like in the question above.

Answer

Here is the basic structure you could use if programming on the Android. This would open an xml file from a web address (R.string.XmlUrlAddress). It would then parse the string and write to another file, probably stored on your Android device?

The commented areas are code that I didn't write rather than comments on what the code is doing. I didn't want to take the time to look up the file io code which should be pretty straight forward.

URL url = new URL(getString(R.string.XmlUrlAddress));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();

NodeList nodeList = doc.getElementsByTagName("Placemark");
String[] files = new String[nodeList.getLength()];

for (int i=0; i < nodeList.getLength(); i++) {
    files[i] = "file" + i + ".xml";
    //Create and open files[i] file.
    //Write opening tag.  "<Placemark>"

    Node node = nodeList.item(i);
    Element element = (Element)node;

    NodeList nameList = element.getElementsByTagName("name");
    Element name = (Element)nameList.item(0);
    nameList = name.getChildNodes();
    String name = nameList.item(0).getNodeValue();
    //Write name to file.  "<name>" + name + "</name>"

    NodeList pointList = element.getElementsByTagName("Point");
    String[] points = new String[pointList.getLength()];

    //Write opening Points tag. "<Points>"
    for (int j=0; j < pointList.getLength(); j++) {
        Node cNode = pointList.item(j);
        Element cElement = (Element)cNode;

        NodeList coordinateList = element.getElementsByTagName("coordinates");
        Element coordinateElement = (Element)coordinateList.item(0);
        coordinateList = coordinateElement.getChildNodes();
        points[j] = coordinateList.item(0).getNodeValue();

        //Write points[j] to file. "<coordinates>" + points[j] + "</coordinates>"
    }
    //Write end Points tag.  "</Points>"
    //Write end document tag.  "</Placemark>"
    //Close file
}
Comments