现有的 XML
<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2013-08-01">
<OperationRequest>
<Arguments>xyz</Arguments>
</OperationRequest>
<Items>
<Item>
<ItemId>123<ItemId>
<CustomerReviews>
<IFrameURL>someurl</IFrameURL>
<HasReviews>true</HasReviews>
</CustomerReviews>
<EditorialReviews>
<Content>text</Content>
</EditorialReviews>
<Item>
</Items>
需要将其转换为
<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2013-08-01">
<OperationRequest>
<Arguments>xyz</Arguments>
</OperationRequest>
<Items>
<Item>
<ItemId>123<ItemId>
<CustomerReviews>
<CustomerReview>
<ReviewText>abc<ReviewText>
<ReviewDate>May 24, 2015<ReviewDate>
</CustomerReview>
<CustomerReview>
<ReviewText>def<ReviewText>
<ReviewDate>June 24, 2014<ReviewDate>
</CustomerReview>
<HasReviews>true</HasReviews>
</CustomerReviews>
<EditorialReviews>
<Content>text</Content>
</EditorialReviews>
<Item>
</Items>
我能够删除 IFrameURL 并能够在 HasReviews 之前插入节点 CustomerReview,但是如何在 CustomerReview 中添加更多节点。 这是我到目前为止使用其他 Stack overflow post 所取得的成就.
<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2013-08-01">
<OperationRequest>
<Arguments>xyz</Arguments>
</OperationRequest>
<Items>
<Item>
<ItemId>123<ItemId>
<CustomerReviews>
<CustomerReview>hello</CustomerReview>
<CustomerReview>hello</CustomerReview>
<CustomerReview>hello</CustomerReview>
<HasReviews>true</HasReviews>
</CustomerReviews>
<EditorialReviews>
<Content>text</Content>
</EditorialReviews>
<Item>
</Items>
PS:XML结构末尾有/ItemLookupResponse,但在代码中不可见。
最佳答案
折腾了一整天,我终于明白了。
这是链接W3Schools ,我曾经完成工作。次要的区别是,使用 item(index) 函数来访问特定的数组位置。例如使用 DOM java 时,以下代码中的第三行可以替换为 x=xmlDoc.getElementsByTagName("book").item(0);
。
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
其他功能也是如此。
关于java - 修改现有的XML,添加和删除节点java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30629611/