Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2003
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    innerxml for mozilla

    Code:
    function maak(strXML){
        var objDOMParser = new DOMParser();
        var objDoc = objDOMParser.parseFromString('<xml>'+strXML+'</xml>', "text/xml");
      
      while (this.hasChildNodes())  this.removeChild(this.lastChild);
    var objImportedNode;
    for (var i=0; i < objDoc.childNodes[0].childNodes.length; i++) {
      switch (objDoc.childNodes[0].childNodes[i].nodeType){
    case 3: objImportedNode = document.createTextNode(objDoc.childNodes[0].childNodes[i].data);
    break;
                  default:           
    objImportedNode = document.importNode(objDoc.childNodes[0].childNodes[i], true);break;
     }
      this.appendChild(objImportedNode);
    } //End: for
      }
      
    function getXML() {
        var objXMLSerializer = new XMLSerializer();
      var temp=this.childNodes.length-1;
      var strXML='';
      for (var i=0;i==temp;i++){
      
      switch (this.childNodes[i].nodeType){
     case 3: strXML+=this.childNodes[i].data.replace(/</g,'&lt;').replace(/>/g,'&gt;');
    break;
      default: strXML+=objXMLSerializer.serializeToString(this.childNodes[i]);
    break;
      
                  }
       
      }
        return strXML;
    }
    
    Node.prototype.__defineGetter__("innerXML", getXML);
    Node.prototype.__defineSetter__("innerXML", maak);
    I've made these functions quite some time ago, and quite possibly this code is quite ugly, but it works.
    Obviously this would be only useful if you are a masochistic xhtml-coder and you want an easy way (kind of contradictory with a masochist) to dynamically add or remove markup/text.

    I needed it for Simon Willison's syntax highlighting experiment:
    http://home.hccnet.nl/m.wargers/test...xHighlight.xml
    Last edited by mw22; 01-13-2004 at 11:56 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    D'oh! I needed something exactly like this a couple of months ago, to do the equivalent of innerHTML in XHTML mode. Too late now, but still interesting
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you please tell me that how can I make following code work.

    var treeXMLStream ="<Classification> <ChildClass> <ClassItem id=\"1\" pid=\"0\" container=\"true\" open=\"false\" label=\"Classified\"/> <ChildClass> <ClassItem id=\"5\" pid=\"1\" label=\"Music\" container=\"true\" open=\"false\"/> <ChildClass> <ClassItem id=\"2\" pid=\"0\" label=\"Eastern\"/> <ChildClass> <ClassItem id=\"5\" pid=\"3\" container=\"false\" open=\"false\" label=\"Eastern Rock\"/> </ChildClass> </ChildClass> <ChildClass> <ClassItem id=\"3\" pid=\"0\" label=\"Western\"/> <ChildClass> <ClassItem id=\"4\" pid=\"3\" container=\"false\" open=\"false\" label=\"Pop\"/> </ChildClass> <ChildClass> <ClassItem id=\"5\" pid=\"3\" container=\"false\" open=\"false\" label=\"Rock\"/> </ChildClass> </ChildClass> </ChildClass> <ChildClass> <ClassItem container=\"true\" open=\"false\" id=\"7\" pid=\"0\" label=\"Art and Calture\"/> <ChildClass> <ClassItem id=\"8\" pid=\"7\" container=\"false\" open=\"false\" label=\"Painting\"/> </ChildClass> </ChildClass></ChildClass> <ChildClass> <ClassItem container=\"true\" open=\"false\" id=\"7\" pid=\"0\" label=\"Selected\"/> <ChildClass> <ClassItem id=\"9\" pid=\"7\" container=\"false\" open=\"false\" label=\"Drama\"/> </ChildClass> </ChildClass> <ChildClass> <ClassItem id=\"10\" pid=\"0\" container=\"false\" open=\"false\" label=\"History\"/> </ChildClass> <ChildClass> <ClassItem id=\"10\" pid=\"0\" container=\"false\" open=\"false\" label=\"Non classified\"/> </ChildClass></Classification>";



    var xmlParser = new DOMParser();

    //parsing from the string
    var classifcationTreeXML = xmlParser.parseFromString(treeXMLStream,"text/xml");

    //and I want to parse the string by Attribute then then by tag name (ChildClass) recursively. But I'm not getting the following result

    var RootClass = classifcationTreeXML.getElementsByAttribute('pid',0);

    //which is not working.confused:

    Plz. tell me how could I get out of this problem

    Thanks,
    Anirban:

  • #4
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,453
    Thanks
    1
    Thanked 21 Times in 19 Posts
    Quote Originally Posted by anirban
    Can you please tell me that how can I make following code work...
    var RootClass = classifcationTreeXML.getElementsByAttribute('pid',0);
    This method belongs to XUL documents, not XML documents. You need a TreeWalker. Ping me by e-mail for help with that.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •