0

I am loading the XML content from a file and want to delete a specific node from this content. Is there a proper way to do it in Nodejs?

For example I have this xmlString:

<sports>
 <cricket Team="England">
  <Players Name="EAA" Flg="0"></Players>
  <Players Name="EAB" Flg="1"></Players>
 </cricket>
 <cricket Team="India">
  <Players Name="IAA" Flg="0"></Players>
  <Players Name="IAB" Flg="1"></Players>
 </cricket>
 <cricket Team="Aus">
  <Players Name="AAA" Flg="0"></Players>
  <Players Name="AAB" Flg="1"></Players>
 </cricket>
</sports>

Here, want to remove cricket.

2
  • cricket is a tag. Are you sure you want to remove that? Commented Aug 8, 2019 at 12:00
  • Yes, its just an example. I want to be able to remove a tag like cricket, or sports, whatever Commented Aug 8, 2019 at 12:07

3 Answers 3

2

Its better to first parse the XML into a javascript object, manipulate it, then convert it back to XML if required, here's a code to delete the "cricket" objects, you'll need to install xml2js to get it working:

    var xml  = `<sports>
 <cricket Team="England">
  <Players Name="EAA" Flg="0"></Players>
  <Players Name="EAB" Flg="1"></Players>
 </cricket>
 <cricket Team="India">
  <Players Name="IAA" Flg="0"></Players>
  <Players Name="IAB" Flg="1"></Players>
 </cricket>
 <cricket Team="Aus">
  <Players Name="AAA" Flg="0"></Players>
  <Players Name="AAB" Flg="1"></Players>
 </cricket>
</sports>`


var parseString = require('xml2js').parseString;
var xml2js = require('xml2js');
parseString(xml, function (err, result) {

  delete result.sports.cricket;
  var builder = new xml2js.Builder();
  var xml = builder.buildObject(result);
    console.log(xml);
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this by using the regex in JavaScript as shown below:

xmlString = '<sports><cricket Team="England"><Players Name="EAA" Flg="0"></Players><Players Name="EAB" Flg="1"></Players></cricket><cricket Team="India"><Players Name="IAA" Flg="0"></Players><Players Name="IAB" Flg="1"></Players></cricket><cricket Team="Aus"><Players Name="AAA" Flg="0"></Players><Players Name="AAB" Flg="1"></Players></cricket></sports>';

console.log(xmlString.replace(/<[//]{0,1}(cricket|\/cricket)[^><]*>/g,""));

3 Comments

I tried it out, but it removes for example: "<sports>", but not "</sports>" So, it have some problems removing </..>. But still, this idea is good
Thanks, its great
Is there also a way to insert a variable in this regex? removeTagByTagName: function (xmlString, tagName) { return xmlString.replace(/<[/]{0,1}(tagName|\/tagName)[^><]*>/g,""); }
0

The code removes xml blocks of certain tag names, e.g. .... and auto close tags .e.g.

usage: removeXmlElements (hay, [...elements])

e.g. const result =removeXmlElements ("hello world", ["b"])

the result will be hello

const hay = `<a>yes yes<w:sdt id="a">hello world</w:sdt>nono</a> 
<w:tcWa id="2"/>aaaa`

const removeXmlElements = (hay, elements) =>{

    const isTagAutoClose = (hay, tag) =>{
       let ret = false
       //situation one with no attributes
       const regexp1 = new RegExp(`<${tag}/>`);
       ret = regexp1.test(hay)
       if (!ret) {
           const regexp2 = new RegExp(`<${tag} [^<>]*?/>`);
           ret = regexp2.test(hay)
       }
       return ret
     }

     let ret = hay    
    
    for (const item of elements) { 
       
        //situation one ele with no attibutes
        const isAutoClose = isTagAutoClose(hay, item)
        if (isAutoClose) {
            const regexp1 = new RegExp(`<${item}/>`, "g");
            ret = ret.replace(regexp1, "")
            //situation two ele with attributes
            const regexp2 = new RegExp(`<${item} [^<>].*?/>`, "gs");
            ret = ret.replace(regexp2, "")

        }else {
            const regexp1 = new RegExp(`<${item}>.*?</${item}>`, "gs");
            ret = ret.replace(regexp1, "")
            //situation two ele with attributes
            const regexp2 = new RegExp(`<${item} [^<>].*?>.*?</${item}>`, "gs");
            ret = ret.replace(regexp2, "")
        }

    }

    return ret
}

console.log(removeXmlElements(hay, ["w:tcWa", "w:sdt"]));


Comments

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.