0

I have an XML and it contains a tag which has a value I want to replace in nodeJS. The XML contains comments and attributes to the tags. Most parsers convert the XML to JSON and do the replace in the process of which, the comments get lost in the final file. So I thought of doing a XML to string conversion and replace and then back to XML. I am unable to convert the string back to XML as many existing libraries in node either are converting to JSON, are unable to to parse attributes with ':' in it. So any Solution for the same is appreciated.

N.B - the XML needs to be exactly the same as earlier in exception to only the one tag value I replace. The order of the tags and the comments are important as there is a checksum calculation involved.

Sample Input :

<?xml version="1.0" encoding="UTF-8"?>
<session abc:def ="dines/dwivedi.../nodeJS/developer" name="Dinesh_Dwivedi" 
samp1:display="sample/text1"
samp2:display="sample/text2" 
samp3:display="sample/text3">
<view name="viewer/name1" type="good.sample.data1">
    <feature ="text1" value="1:1"/>
    <!--This is a sample comment-->
</view>

<view name="viewer/name2" type="good.sample.data2">
    <feature ="text2" value="1:2"/>
    <!--This is a sample comment too-->
</view>

  <!--again another Sample comment--> 
</session>

Text to change - Dinesh_Dwivedi

Replace Text - Dines_Dwivedi

Final Output -

<?xml version="1.0" encoding="UTF-8"?>
<session abc:def ="dines/dwivedi.../nodeJS/developer" name="Dines_Dwivedi" 
samp1:display="sample/text1"
samp2:display="sample/text2" 
samp3:display="sample/text3">
<view name="viewer/name1" type="good.sample.data1">
    <feature ="text1" value="1:1"/>
    <!--This is a sample comment-->
</view>

<view name="viewer/name2" type="good.sample.data2">
    <feature ="text2" value="1:2"/>
    <!--This is a sample comment too-->
</view>

  <!--again another Sample comment--> 
</session>
  • If you can't find a parser, for a change that predictable and trivial, you *could* use a simple `.replace` – CertainPerformance Aug 29 '18 at 06:36
  • `Most parsers convert the XML to JSON`. If you believe that, you've been looking in the wrong place. Most parsers convert the XML to a tree of nodes (typically a DOM tree) which can then be serialized back as XML losing almost nothing (the exceptions are trivial details like the choice of single or double quotes around attributes). – Michael Kay Aug 29 '18 at 07:53
  • @MichaelKay Thanks for the info. Any particular parser in mind that can do that. – Dines Dwivedi Aug 29 '18 at 08:06
  • @CertainPerformance I used .replace on the string generated but to convert the replaced text back to XML is where I am stuck. – Dines Dwivedi Aug 29 '18 at 08:07
  • You can parse the XML file *as a string* and replace as needed - there shouldn't be a need for anything else, I would think – CertainPerformance Aug 29 '18 at 08:08
  • Sadly, the SO powers-that-be have decided that requests to find a library to do X are off topic ("not about programming", though some would say it's a programmer's key task...). But one off-topic question that discusses choice of XML parsers for node.js is at https://stackoverflow.com/questions/14890655/the-best-node-module-for-xml-parsing – Michael Kay Aug 29 '18 at 08:12
  • @CertainPerformance thanks it worked. – Dines Dwivedi Aug 30 '18 at 05:55
  • @MichaelKay thanks for all the info. It was educative. – Dines Dwivedi Aug 30 '18 at 05:56

0 Answers0