Modify existing XML, adding and deleting nodes java -
existing 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> need convert to
<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> i able remove iframeurl , able insert node customerreview before hasreviews, how can add more nodes inside customerreview. here have achieved till using other 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 structure have /itemlookupresponse @ end, not visible in code.
after spending whole day, figured out.
here link w3schools, used work done. minor difference is, use item(index) function access particular array position. e.g. third line in below code can replaced x=xmldoc.getelementsbytagname("book").item(0); while using dom java.
xmldoc=loadxmldoc("books.xml"); newel=xmldoc.createelement("edition"); x=xmldoc.getelementsbytagname("book")[0]; x.appendchild(newel); same goes other functionalities.
Comments
Post a Comment