简体   繁体   English

如何使用 StAX 合并具有特定条件的两个 XML 文件

[英]How to merge two XML files with certain condition using StAX

For example, let's say we have the two XML files:例如,假设我们有两个 XML 文件:

"1.xml" “1.xml”

<School>
   <Stdent id="1">
      <Name>Jhon</Name>
      <Age>12</Age>
      <Grade>7</Grade>
   </Student>
   <Stdent id="2">
      <Name>Mike</Name>
      <Age>11</Age>
      <Grade>8</Grade>
   </Student>
</School>

"2.xml" “2.xml”

<School>
   <Stdent id="1">
      <Name>Jhon</Name>
      <Age>13</Age>
      <Grade>9</Grade>
   </Student>
   <Stdent id="3">
      <Name>Ann</Name>
      <Age>11</Age>
      <Grade>10</Grade>
    </Student>
</School>

For the merged file the condition is if the student id in the second file matches one in the first file put it from the second file.对于合并文件,条件是如果第二个文件中的学生 ID 与第一个文件中的学生 ID 匹配,则将其从第二个文件中取出。 The merged file should look like this:合并后的文件应如下所示:

"merge.xml" “合并.xml”

<School>
   <Stdent id="1">
      <Name>Jhon</Name>
      <Age>13</Age>
      <Grade>9</Grade>
   </Student>
   <Stdent id="2">
      <Name>Mike</Name>
      <Age>11</Age>
      <Grade>8</Grade>
   </Student>
   <Stdent id="3">
      <Name>Ann</Name>
      <Age>11</Age>
      <Grade>10</Grade>
    </Student>
</School>

How should I do it?我该怎么做? Thanks in advance!提前致谢!

If you're flexible about the technology you can use, here's the XSLT 3.0 solution:如果您对可以使用的技术很灵活,这里是 XSLT 3.0 解决方案:

<xsl:merge>
  <xsl:merge-source for-each-source="'xml1.xml', 'xml2.xml'"
                    select="//Stdent">
    <xsl:merge-key select="@id"/>
  </xsl:merge-source>
  <xsl:merge-action>
    <xsl:copy-of select="current-merge-group()[last()]"/>
  </xsl:merge-action>
</xsl:merge>

Your question is tagged with Java, so I'm assuming it's what you're using.你的问题用 Java 标记,所以我假设它是你正在使用的。

There are two ways to go about it, one is with JAXB - which is a standard Java library facilitating mapping of XML to and from Java objects.有两种方法可以解决这个问题,一种是使用 JAXB——它是一个标准的 Java 库,可以促进 XML 与 Java 对象之间的映射。 Your files look structured, so that's what I would recommend.您的文件看起来很有条理,所以这就是我的建议。 You can create a model class based on your XML and use this library to convert your files to a Java object.您可以基于 XML 创建模型类并使用该库将文件转换为 Java 对象。 You can also generate a schema based on the XML files and pass that schema to the JAXB and it will generate the model classes for you.您还可以基于 XML 文件生成模式并将该模式​​传递给 JAXB,它会为您生成模型类。

The second option is to parse the files using the DocumentBuilder class and manually insert nodes.第二个选项是使用DocumentBuilder类解析文件并手动插入节点。

Both of these tools are well-documented and can be easily looked up online so I will not paste code examples.这两个工具都有详细的文档,可以很容易地在线查找,所以我不会粘贴代码示例。

I would not recommend the XSLT approach that someone else suggested for something that simple.我不会推荐其他人为这么简单的事情建议的 XSLT 方法。 XSLT is hard to read and maintain, and not that commonly used. XSLT 难以阅读和维护,而且不常用。 Manipulating XMLs programmatically is bound to come handy at some point anyway.无论如何,以编程方式操作 XML 肯定会在某些时候派上用场。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM