I have a huge xml structure which i have attached below,
I am adding update details and adding data to the product tracking module but when i try to change it it will replacing the entire xml with the new xml Sample xml
<query_by_gtin_response:queryByGtinResponse xmlns:query_by_gtin_response="urn:gs1:tsd:query_by_gtin_response:xsd:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:gs1:tsd:query_by_gtin_response:xsd:1 QueryByGtinResponse.xsd urn:gs1:tsd:basic_product_information_module:xsd:1 BasicProductInformationModule.xsd urn:gs1:tsd:nutritional_product_information_module:xsd:1 NutritionalProductInformationModule.xsd">
<productData>
<gtin>00000000000509</gtin>
<targetMarket>124</targetMarket>
<informationProviderGLN>1111111133333</informationProviderGLN>
<informationProviderName>Testsub</informationProviderName>
<productDataRecord>
<module>
<bpi:basicProductInformationModule xmlns:bpi="urn:gs1:tsd:basic_product_information_module:xsd:1">
<productName languageCode="en">waka waka</productName>
<brandNameInformation>
<brandName>wowozella</brandName>
</brandNameInformation>
</bpi:basicProductInformationModule>
</module>
<module>
<nfii:nonfoodIngredientInformationModule xmlns:nfii="urn:gs1:tsd:nonfood_ingredient_information_module:xsd:1"/>
</module>
<module>
<product_tracking_information_module>
<variantId>ec9ef090-57c2-11e6-bc2b-51c4bf71f613</variantId>
<status/>
<name>admin@gmail.com</name>
<createdBy>552f5b90b348147e03e49b62</createdBy>
<createdDate>2016-08-01T08:35:38.648Z</createdDate>
<updatedBy>552f5b90b348147e03e49b62</updatedBy>
<updatedDate>2016-08-01T08:35:38.648Z</updatedDate>
<applicationId>webapp</applicationId>
<history>
<createdDate>2016-08-01T08:35:38.648Z</createdDate>
<updatedDate>2016-08-01T08:35:38.648Z</updatedDate>
<status/>
<updatedBy>552f5b90b348147e03e49b62</updatedBy>
<createdBy>552f5b90b348147e03e49b62</createdBy>
<applicationId>webapp</applicationId>
<name>admin@gmail.com</name>
</history>
</product_tracking_information_module>
</module>
</productDataRecord>
</productData>
</query_by_gtin_response:queryByGtinResponse>
the sample xml i want to replace the current full data in product_tracking_information_module with below
<product_tracking_information_module>
<variantId>ec9ef090-57c2-11e6-bc2b-51c4bf71f613</variantId>
<status>deleted</status>
<name>gs1canada</name>
<createdBy>552f5b90b348147e03e49b62</createdBy>
<createdDate>2016-08-01T08:35:38.648Z</createdDate>
<updatedBy>company Id 552f5b62b348147e03e49b61</updatedBy>
<updatedDate>2016-08-01T14:52:23.985Z</updatedDate>
<applicationId>serverlet</applicationId>
<history>
<createdDate>2016-08-01T08:35:38.648Z</createdDate>
<updatedDate>2016-08-01T08:35:38.648Z</updatedDate>
<status/>
<updatedBy>552f5b90b348147e03e49b62</updatedBy>
<createdBy>552f5b90b348147e03e49b62</createdBy>
<applicationId>webapp</applicationId>
<name>admin@gmail.com</name>
</history>
<history status="deleted">
<applicationId/>
<updatedBy>company Id 552f5b62b348147e03e49b61</updatedBy>
<updatedDate>2016-08-01T14:52:23.985Z</updatedDate>
<name>gs1canada</name>
</history>
</product_tracking_information_module>
and the code to do that, where doc is the xmldom parsed Document
var ptimdom = new dom().parseFromString(ptimxml.toString());
var nodesPTIM = xpath.select("//productData//productDataRecord//module//product_tracking_information_module", doc);
doc.replaceChild(ptimdom,nodesPTIM[0]);
but the result is that the doc after this is the second xml only and the full data is lost.
What am i doing wrong?
Try nodesPTIM[0].parentNode.replaceChild(ptimdom, nodesPTIM[0])
, that is call the replaceChild method on the parentNode of the element you want to replace.