2

I have a XML document that looks like this:

 <Data id="1401" href="http://222222222">
  <definition id="12218" href="http://11"></definition>
  <Data-List count="1">
    <DataStep type="3">
      <completed_time>07/04/2017 17:18:11</completed_time>
      <status>3</status>
    </DataStep>
  </Data-List>
  <information>abcdefg</information>
</Data>

I would like to change the information data abcdefg to a different piece of string that comes from a different variable.

So it looks like this:

 <Data id="1401" href="http://222222222">
  <definition id="12218" href="http://11"></definition>
  <Data-List count="1">
    <DataStep type="3">
      <completed_time>07/04/2017 17:18:11</completed_time>
      <status>3</status>
    </DataStep>
  </Data-List>
  <information>example</information>
</Data>

Is this possible to do directly in Node JS without changing this XML to a json, modifying it and then converting back to XML? I have tried this however it did not seem to work, creating weird arrays and '$' on the XML conversion.

Any suggestions on how I could do this?

5
  • 1
    Do you parse the XML or do you read the file as a string and try to replace the abcdefg by example? Commented Jul 4, 2017 at 18:20
  • Its read as a string - however abcdefg and example will change each time Commented Jul 4, 2017 at 18:20
  • Use something like github.com/Leonidas-from-XIV/node-xml2js to parse XML then modify easily Commented Jul 4, 2017 at 18:23
  • @ArpitSolanki but then I need to convert it back to XML, which is where things go wrong as described above Commented Jul 4, 2017 at 18:24
  • It won&#39;t go wrong. If it can convert it to json then it can convert json back to XML Commented Jul 4, 2017 at 18:27

2 Answers 2

1

I would use cherriojs https://github.com/cheeriojs/cheerio .

Itis the nodejs implemetation of jQuery so DOM manipulations are easy. At the top of your file you have to set the xmlMode:true after you require it.

var $ = cheerio.load(your-xml-doc, {
          xmlMode: true
        });

Then you can use something like

$('information').text('example');
Sign up to request clarification or add additional context in comments.

3 Comments

I do not use JQuery? I use Node JS
You npm install cherrio then require it at the top of the file. Then all the DOM methods form jQuery are available in nodejs. It is the simplest method, just look at the docs in the link. They are very well written.
Seconded for cheerio. It's my go-to for quick XML manipulation.
0

1)You can parse your xml document like a normal string, find information tag with regular expression and change it.

xml = xml.replace(/<information>[a-z0-9_-]*<\/information\>/, '<information>example</information>');

2)You can use some module, like xmldoc, to parse the document. https://github.com/nfarina/xmldoc

let xmldoc = require('xmldoc');
let document = new xmldoc.XmlDocument(data.toString());
document.descendantWithPath('information').val = "example";

2 Comments

How would I use regular expression on this to modify it? Im a beginner to coding so this does not mean much to me
@angels some code snippet or example would be great to make this as a complete answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.