在java中合并XML,多个节点的标记名称与子节点相同?

时间:2016-10-21 05:43:34

标签: java xml merge tagname

我在合并两个XML字符串或文件时遇到问题。

XML1。

<?xml version="1.0" encoding="UTF-8"?>
<Specification>
	<item>
		<id>PS1</id>
		<startDate> 21 Oct 2016</startDate>
		<Product>
			<item>
				<id>P1</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
			<item>
				<id>P2</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
		</Product>
		<CompositePrice>
			<item>
				<id>PR1</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>1.0</Price>
			</item>
			<item>
				<id>PR2</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>2.0</Price>
			</item>
		</Price>
	</item>
	<item>
		<id>PS2</id>
		<startDate> 21 Oct 2016</startDate>
		<Product>
			<item>
				<id>P3</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
			<item>
				<id>P4</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
		</Product>
		<CompositePrice>
			<item>
				<id>PR3</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>1.0</Price>
			</item>
			<item>
				<id>PR4</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>2.0</Price>
			</item>
		</Price>
	</item>
</Specification>

XML2。

<?xml version="1.0" encoding="UTF-8"?>
<Specification>
	<item>
		<id>PS3</id>
		<startDate> 21 Oct 2016</startDate>
		<Product>
			<item>
				<id>P5</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
			<item>
				<id>P6</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
		</Product>
		<CompositePrice>
			<item>
				<id>PR5</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>1.0</Price>
			</item>
			<item>
				<id>PR6</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>2.0</Price>
			</item>
		</Price>
	</item>
	<item>
		<id>PS1</id>
		<startDate> 21 Oct 2016</startDate>
		<Product>
			<item>
				<id>P7</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
			<item>
				<id>P8</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
		</Product>
		<CompositePrice>
			<item>
				<id>PR3</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>1.0</Price>
			</item>
			<item>
				<id>PR4</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>2.0</Price>
			</item>
		</Price>
	</item>
</Specification>

预期产出:

<?xml version="1.0" encoding="UTF-8"?>
<Specification>
	<item>
		<id>PS1</id>
		<startDate> 21 Oct 2016</startDate>
		<Product>
			<item>
				<id>P1</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
			<item>
				<id>P2</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
		</Product>
		<CompositePrice>
			<item>
				<id>PR1</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>1.0</Price>
			</item>
			<item>
				<id>PR2</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>2.0</Price>
			</item>
		</Price>
	</item>
	<item>
		<id>PS2</id>
		<startDate> 21 Oct 2016</startDate>
		<Product>
			<item>
				<id>P3</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
			<item>
				<id>P4</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
		</Product>
		<CompositePrice>
			<item>
				<id>PR3</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>1.0</Price>
			</item>
			<item>
				<id>PR4</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>2.0</Price>
			</item>
		</Price>
	</item>
	<item>
		<id>PS3</id>
		<startDate> 21 Oct 2016</startDate>
		<Product>
			<item>
				<id>P5</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
			<item>
				<id>P6</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
		</Product>
		<CompositePrice>
			<item>
				<id>PR5</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>1.0</Price>
			</item>
			<item>
				<id>PR6</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>2.0</Price>
			</item>
		</Price>
	</item>
	<item>
		<id>PS1</id>
		<startDate> 21 Oct 2016</startDate>
		<Product>
			<item>
				<id>P7</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
			<item>
				<id>P8</id>
				<startDate> 21 Oct 2016</startDate>
			</item>
		</Product>
		<CompositePrice>
			<item>
				<id>PR3</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>1.0</Price>
			</item>
			<item>
				<id>PR4</id>
				<startDate> 21 Oct 2016</startDate>
				<Price>2.0</Price>
			</item>
		</Price>
	</item>
</Specification>

我尝试运行java代码,但是当第二个代码在“规范”下有多个项目时无法合并。 当第二个只有单个项目时运行正常,即使首次有多个项目在规范下。

0 个答案:

没有答案