PDA

View Full Version : innerxml for mozilla


mw22
01-13-2004, 11:53 PM
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/mozilla/syntaxHighlight.xml

brothercake
01-14-2004, 10:35 AM
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 :)

anirban
11-16-2005, 12:12 PM
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:

Alex Vincent
11-16-2005, 09:06 PM
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.