gpt4 book ai didi

javascript - 在node.js中使用xpath和xmldom替换部分xml

转载 作者:行者123 更新时间:2023-12-03 06:24:50 25 4
gpt4 key购买 nike

我有一个巨大的 xml 结构,我已将其附加在下面,

我正在添加更新详细信息并向产品跟踪模块添加数据,但是当我尝试更改它时,它将用新的 xml 替换整个 xml示例 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>

我想用下面的示例 xml 替换 Product_tracking_information_module 中当前的完整数据

<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>

以及执行此操作的代码,其中 doc 是 xmldom 解析的文档

var ptimdom = new dom().parseFromString(ptimxml.toString());
var nodesPTIM = xpath.select("//productData//productDataRecord//module//product_tracking_information_module", doc);
doc.replaceChild(ptimdom,nodesPTIM[0]);

但结果是此后的文档只是第二个 xml,并且完整数据丢失。

我做错了什么?

最佳答案

尝试nodesPTIM[0].parentNode.replaceChild(ptimdom,nodesPTIM[0]),即在要替换的元素的parentNode上调用replaceChild方法。

关于javascript - 在node.js中使用xpath和xmldom替换部分xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38702066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com