0

I have a question: I'm getting in Javascript XML. I want to add a 'father' node to that xml. How do I do that?

/* Load the XML text from the text area to a Javascript XML object */
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(taData.innerText);
xmlObj = xmlDoc.documentElement;        

/* Creating the Screen node */
var Screen = document.createElement("Screen");
Screen.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
Screen.setAttribute("xsi:noNamespaceSchemaLocation", "../../GUIGenerator_V2/Scheme/GG_Scheme.xsd");

/* Creating the Legend node */
var Legend = document.createElement("Legend");
Legend.setAttribute("EntityType", "Request");

var ImportedNode = document.adopteNode(xmlDoc.documentElement);
Legend.appendChild(ImportedNode);
Screen.appendChild(Legend);

Legend is the child of Screen, And I want to make the xmlDoc a child of Legend.

I have tried to write: Legend.appendChild(xmlDoc.documentElement); but getting an error. What can be the problem?

4
  • Could you provide some example code? Did you already parse the XML into a XMLDocument? What exactly do you want to do? Commented Feb 28, 2012 at 14:48
  • That does not seem to make sense to me... a document cannot be the child of a node. Either you add the root of the document as child or you serialize the document and set it as content of Legend (as a string). Commented Feb 28, 2012 at 14:52
  • how do I add the root element? Commented Feb 28, 2012 at 14:53
  • Can some one help me solving this? Commented Feb 29, 2012 at 6:40

1 Answer 1

1

In some case, a XML is reference as a DOM inside JavaScript so you can use standard DOM functions on it. Pay attention about navigator specific implementation to avoid compatibility problems...

To add a father node you need to use something like :

/* Load the XML text from the text area to a Javascript XML object */
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML(taData.innerText);
    xmlObj = xmlDoc.documentElement;        

    /* Creating the Screen node */
    var Screen = document.createElement("Screen");
    Screen.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    Screen.setAttribute("xsi:noNamespaceSchemaLocation", "../../GUIGenerator_V2/Scheme/GG_Scheme.xsd");

    /* Creating the Legend node */
    var Legend = document.createElement("Legend");
    Legend.setAttribute("EntityType", "Request");

    var ImportedNode = document.adopteNode(xmlDoc.documentElement);    
    Legend.appendChild(ImportedNode);
    Screen.appendChild(Legend);

after execution of that code you obtain a document strucured like:

<fathernode>
  <YOURXMLDOCUMENT />
</fathernode>
Sign up to request clarification or add additional context in comments.

8 Comments

I have tried to write: Legend.appendChild(xmlDoc.documentElement);
Object doesn't support this property or method
I have copied to code I'm trying to run to the original message
I'm still getting the same error, Why do you create the 'legend' element twice?
I have updated the code I'm trying to run, but still getting the same error
|

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.