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文件,其中包含多个图片的数据,如GPS坐标,日期/时间和一些我需要分成几个XML文件的图像数据。
<?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
.....我读到了这个问题:在多个XML文件中拆分XML
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.
并尝试在导入所有内容后为我的文件稍微修改一下代码。想知道是否有人对如何修改我的代码进行吐痰有任何好的想法,就像上面的问题一样。
4 个解决方案
#1
0
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?
这是在Android上编程时可以使用的基本结构。这将从Web地址(R.string.XmlUrlAddress)打开一个xml文件。它会解析字符串并写入另一个文件,可能存储在您的Android设备上?
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.
注释区域是我没有写的代码,而不是代码正在做什么的评论。我不想花时间查找文件io代码,这应该是非常简单的。
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
}
#2
3
It's very simple in XSLT 2.0:
在XSLT 2.0中它非常简单:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
<xsl:template match="Placemark">
<xsl:result-document href="Placemark{position()}.xml">
<xsl:next-match/>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
#3
0
It's possible that there is a better way, but you could look into a SAX XML Parser, and then parse to the place that you want to cut the file at, and cut the file (make sure you end with the proper tags, and then start the tags again for the next file otherwise you will have 2 invalid xml files).
有可能有更好的方法,但您可以查看SAX XML Parser,然后解析到要剪切文件的位置,并剪切文件(确保以适当的标签结束,然后再次为下一个文件启动标记,否则将有2个无效的xml文件)。
#4
0
This is very old question, but maybe this will help to somebody. In SO are a lot of answers etc, so I tried to made something universal. Here is my simple SWING app which saved me. I had a huge file where was PRODUCT as root element and Products as item elements. So if somebody need to load structure, select root element (which will be in all files) and item element which would be surround by root element, this can help you.
这是一个非常古老的问题,但也许这会对某人有所帮助。在SO中有很多答案等,所以我试图做出一些普遍的东西。这是我的简单SWING应用程序,它救了我。我有一个巨大的文件,其中PRODUCT作为根元素,Products作为项目元素。因此,如果有人需要加载结构,选择根元素(将在所有文件中)和项元素将由根元素包围,这可以帮助您。
#1
0
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?
这是在Android上编程时可以使用的基本结构。这将从Web地址(R.string.XmlUrlAddress)打开一个xml文件。它会解析字符串并写入另一个文件,可能存储在您的Android设备上?
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.
注释区域是我没有写的代码,而不是代码正在做什么的评论。我不想花时间查找文件io代码,这应该是非常简单的。
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
}
#2
3
It's very simple in XSLT 2.0:
在XSLT 2.0中它非常简单:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
<xsl:template match="Placemark">
<xsl:result-document href="Placemark{position()}.xml">
<xsl:next-match/>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
#3
0
It's possible that there is a better way, but you could look into a SAX XML Parser, and then parse to the place that you want to cut the file at, and cut the file (make sure you end with the proper tags, and then start the tags again for the next file otherwise you will have 2 invalid xml files).
有可能有更好的方法,但您可以查看SAX XML Parser,然后解析到要剪切文件的位置,并剪切文件(确保以适当的标签结束,然后再次为下一个文件启动标记,否则将有2个无效的xml文件)。
#4
0
This is very old question, but maybe this will help to somebody. In SO are a lot of answers etc, so I tried to made something universal. Here is my simple SWING app which saved me. I had a huge file where was PRODUCT as root element and Products as item elements. So if somebody need to load structure, select root element (which will be in all files) and item element which would be surround by root element, this can help you.
这是一个非常古老的问题,但也许这会对某人有所帮助。在SO中有很多答案等,所以我试图做出一些普遍的东西。这是我的简单SWING应用程序,它救了我。我有一个巨大的文件,其中PRODUCT作为根元素,Products作为项目元素。因此,如果有人需要加载结构,选择根元素(将在所有文件中)和项元素将由根元素包围,这可以帮助您。