English 中文(简体)
将XML列入多个xml档案
原标题:Splitting XML into multiple xml files

我有XML文档,有多图像数据,如全球定位系统坐标、日期/时间,以及我需要分成若干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>

我想谈谈:

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.

最佳回答

这里,如果在安的安康方案拟定工作,你可以使用的基本结构。 这将从网上地址(R.string.XmlUrl Address)上打开Xml文档。 然后,它会把扼杀和写到另一个档案中,可能储存在你的安乐器上?

评论的领域是我没有撰写的法典,而没有评论守则正在做什么。 我不想花时间来研究应该直截了当的档案。

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
}
问题回答

缩略语

<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>

也许可以找到更好的办法,但你可以看一看SAX XML Parser,然后把你想要切断档案和切断档案的地方(确保你用适当的标签结束,然后重新开始对下一个档案进行标记,否则就会有2个无效xml文档)。

这是一个老的问题,但也许会帮助一些人。 在SO中,有许多答案等等,因此,我试图做到普及。 Here是我挽救我的简单全家用电器。 我拥有一个巨大的档案,即《防止母婴传播公约》作为基本内容和产品。 因此,如果有些人需要装满结构,选择根本要素(在所有档案中都是如此)和以根本内容为重的物品要素,就可以帮助你。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签