How to remove all XML nodes having specified attributes.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viKasm
    New Member
    • Aug 2010
    • 3

    How to remove all XML nodes having specified attributes.

    Hi All,

    I want to remove all XML nodes having specified attributes.

    my XML:
    Code:
    var strText='<p>My First Line.<br /><span ignoreIn="true">Ignore in true.</span>My Second Line.<br /><span ignoreIn="false">Ignore <span ignoreIn="true">Ignore in true.</span>in false.</span>My Third Line.<br /><span ignoreIn="true">Ignore in true.</span>My Fourth Line.<br />My Last Line.</p>';
    I want an output like: '
    Code:
    <p>My First Line.<br />My Second Line.<br /><span ignoreIn="false">Ignore in false.</span>My Third Line.<br />My Fourth Line.<br />My Last Line.</p>
    Can any one of you please help me to get the javascript code?
    Last edited by gits; Aug 2 '10, 02:30 PM. Reason: added code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    basically, load the string as XML, get all <span>s, remove those who have an ignoreIn attribute, which is set to "true".

    Comment

    • viKasm
      New Member
      • Aug 2010
      • 3

      #3
      Originally posted by Dormilich
      basically, load the string as XML, get all <span>s, remove those who have an ignoreIn attribute, which is set to "true".
      Thanks Dormilich for your quick reply!

      I have written a code as follows:
      Code:
      strText = '<p>My First Line.<br /><span ignoreIn="true">Ignore in true.</span>My Second Line.<br /><span ignoreIn="false">Ignore <span ignoreIn="true">Ignore in true.</span>in false.</span>My Third Line.<br /><span ignoreIn="true">Ignore in true.</span>My Fourth Line.<br />My Last Line.</p>';
      //
      function loadXMLString (p_strText) {
      if (window.DOMParser) {
      parser = new DOMParser ();
      xmlTempDoc = parser.parseFromString (p_strText,"text/xml");
      } else {
      xmlTempDoc = new ActiveXObject ("Microsoft.XMLDOM");
      xmlTempDoc.async = "false";
      xmlTempDoc.loadXML (p_strText);
      }
      return xmlTempDoc;
      }
      //
      function removeIgnoreInFlashFreeNodes (p_strXMLData) {
      for (var i = 0; i < p_strXMLData.childNodes.length; i++) {
      var strChild = p_strXMLData.childNodes[i];
      if (strChild.nodeName == 'span') {
      var removeThisNode = false;
      if (strChild.getAttribute ("ignoreIn") == "true") {
      xmlDoc.documentElement.removeChild (xmlDoc.getElementsByTagName ("span")[i]); 
      removeThisNode = true;
      }
      if  (removeThisNode) {
      i--;
      continue;
      }
      }
      if  (strChild.hasChildNodes ()) {
      removeIgnoreInFlashFreeNodes (strChild);
      }
      }
      }
      xmlDoc = loadXMLString (strText);
      removeIgnoreInFlashFreeNodes (xmlDoc);
      But the code is not working properly.
      Last edited by Dormilich; Aug 2 '10, 03:06 PM. Reason: Please use [code] tags when posting code

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        why using childNodes, when you can access the spans via getElementsByTa gName()?

        PS.
        Code:
        function removeNode(elem)
        {
            elem.parentNode.removeChild(elem);
        }

        Comment

        • viKasm
          New Member
          • Aug 2010
          • 3

          #5
          Thanks a lot Dormilich!

          It works by using getElementsByTa gName.
          :)

          Comment

          Working...