简体   繁体   English

在node.js中使用xpath和xmldom替换xml的一部分

[英]replace a portion of xml using xpath and xmldom in node.js

I have a huge xml structure which i have attached below, 我在下面附加了一个巨大的xml结构,

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

the sample xml i want to replace the current full data in product_tracking_information_module with below 我想用下面的示例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>

and the code to do that, where doc is the xmldom parsed Document 以及执行此操作的代码,其中doc是xmldom解析的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. 但是结果是此后的文档仅是第二个xml,并且所有数据都丢失了。

What am i doing wrong? 我究竟做错了什么?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM