我现有的 xml 文件看起来像
<transaction>
<id>
<in>computer</in>
<sn>1234567</sn>
<book>JAVA</book>
<author>klen</author>
</id>
<data>
<dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
<key>Err</key>
</data>
</transaction>
但我想在 <id>
节点之前添加节点。现在我有 2 个问题
1.使用dom perser之前如何添加节点?
2. 除了 dom 解析器之外,是否可以这样做?因为我给出了一个 demo xml 原始 xml 很大,所以我需要一个好的解析器以获得更好的性能。
我要添加这个
<xs:schema id="transaction" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="transaction" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="id">
<xs:complexType>
<xs:sequence>
<xs:element name="in" type="xs:string" minOccurs="0" />
<xs:element name="sn" type="xs:string" minOccurs="0" />
<xs:element name="book" type="xs:string" minOccurs="0" />
<xs:element name="author" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="productData">
<xs:complexType>
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
在<id>
节点下的<transaction>
节点之前。
最佳答案
问题一:“使用dom perser如何在之前添加节点?”
答案 1:
我假设您正在使用 org.w3c.dom .设 node1
为 Node在你的文档中。让 node2
成为另一个 Node在你的文档中。你想insert node1 before node2 .
问题 2:“除了 dom 解析器之外,是否可以执行此操作?”
答案 2:DOM 不是 XML 的当前技术水平。您应该研究 XSLT、SAX 和/或 JAXB。
关于java - 如何在java中的任何特定节点之前添加xml中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9966551/