0

I have a XML similar to following which I am processing in JavaScript.

<doc>
  <entry>
    <child1>
     <child2>
      <child3 n="a">
      </child3>
    </child2>
   </child1>
  </entry>

  <entry>
   <child1>
     <child2>
      <child3 n="b">
      </child3>
    </child2>
   </child1>
  </entry>

  <entry>
   <child1>
     <child2>
      <child3 n="c">
      </child3>
    </child2>
   </child1>
  </entry>
</doc>

If child3, n = "c", I need to remove the whole <entry> tag. My problem is if I do it in a normal loop, the index changes and it doesn't work.

So my question is, how can i remove each where one of the child3, n == "c" - There can be multiple of each entry where child3 n = "c".

bhavicp
  • 336
  • 1
  • 4
  • 15
  • How are you processing this document? XSLT, Python, C#, Java? Also that document isn't valid, as there are no closing tags. Are the child elements nested inside each other, it looks like every element in that is nested in the one prior. –  Apr 08 '14 at 01:12
  • Hi, I've fixed the XML - Sorry about that. I'm using Javascript. – bhavicp Apr 08 '14 at 01:53
  • I'd recommend adding in the Javascript code you are using as well. –  Apr 08 '14 at 01:54

1 Answers1

1

I'd use an xpath

//entry[*/*/*[@n='c']]

to find what you're looking for, then remove it:

node.parentNode.removeChild(node);

Here's a working example that removes all nodes with n='c' attributes (try it here):

var xmlStr = "<doc><entry><child1> <child2> <child3 n='a'> </child3> </child2> </child1> </entry>";
xmlStr += "<entry> <child1> <child2> <child3 n='c'> </child3> </child2> </child1> </entry>";
xmlStr += "<entry> <child1> <child2> <child3 n='b'> </child3> </child2> </child1> </entry>";
xmlStr += "<entry> <child1> <child2> <child3 n='d'> </child3> </child2> </child1> </entry>";
xmlStr += "<entry> <child1> <child2> <child3 n='c'> </child3> </child2> </child1> </entry>";
xmlStr += "</doc>";

var xmlToString = function(oXML) {
  if (window.ActiveXObject) {
    return oXML.xml;
  } else {
    return (new XMLSerializer()).serializeToString(oXML);
  }
}

//From: http://stackoverflow.com/a/8412989/83418
var parseXml;
if (typeof window.DOMParser != "undefined") {
    parseXml = function(xmlStr) {
        return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" &&
       new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    throw new Error("No XML parser found");
}

var xpath = "//entry[*/*/*[@n='c']]";
var xml = parseXml(xmlStr);
var nodes = xml.evaluate(xpath,xml,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
for ( var i=0 ; i < nodes.snapshotLength; i++) {
    var node = nodes.snapshotItem(i);
    node.parentNode.removeChild(node);
}
var updatedXml = xmlToString(xml);
alert(updatedXml);
Jeffrey Knight
  • 5,888
  • 7
  • 39
  • 49