?
Solved

Manipulating XML with the DOM

Posted on 2006-07-17
5
Medium Priority
?
333 Views
Last Modified: 2013-11-19
Hello,

I need to take a String of XML and insert a tag into it. I'm trying to parse the XML to a Document and then insert the tag, but failing.

The XML Document looks like this:

<sql>
      <request name="name of request">
            <param type="paramtype">foo</param>
            <param type="paramtype">bar</param>
      </request>
</sql>

And I need to insert another param after all the others: <param type="paramtype">baz</param>

I'm using the Sarissa library which emulates Mozilla's XML capability for IE. This is what I have so far:

function insertParam (query,param,type) {
      var oDomDoc = Sarissa.getDomDocument();
      oDomDoc = doParseXML(query); //this function just uses the Sarissa library to parse the String into a DOM Document (this bit works)
      
      var newparam = document.createElement("param");
      newparam.setAttribute("type",type);
      
      //this won't work in mozilla, but will in IE
      newparam.text = param;
      
      var request = oDomDoc.getElementsByTagName("request")[0];
      request.appendChild(newparam);
      
      return oDomDoc;
}


The above function bails out when you call appendChild, and I don't know why. In addition, I need a cross-browser solution, so the IE-only .text value can't really be used either. The above function would also add the new param above all the others, rather than at the end of the list.

I've tried picking my way through w3schools DOM pages, but I just can't figure it out.

Many thanks in advance,
Matt.
0
Comment
Question by:mb2297
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:zeroreality
ID: 17132077
instead of:
var newparam = document.createElement("param");
try:
var newparam = oDomDoc.createElement("param");

instead of:
newparam.text = param;
try:
newparam.nodeValue = param;
0
 

Author Comment

by:mb2297
ID: 17132455
Nope - same problems.

nodeValue is not defined for Nodes of type Element (newparam.nodeType gives you 1). See http://mozref.com/reference/objects/Node

This is getting urgent so I've raised the point value. Someone out there must know how to do this - it doesn't seem that hard.

Thanks,
Matt.
0
 
LVL 2

Expert Comment

by:zeroreality
ID: 17133487
can you post a link to the project so we can see all the code?

have you also tried:
newparam.value = param;
or:
newparam.innerXML = oDomDoc.createTextNode(param);
0
 
LVL 2

Accepted Solution

by:
zeroreality earned 1500 total points
ID: 17133735
or even:
newparam.appendChild(oDomDoc.createTextNode(param));
0
 

Author Comment

by:mb2297
ID: 17134982
Crikey,

I finally fixed it. The function that works is:

            function insertParam (query,param,type) {
            
                   var oDomDoc = Sarissa.getDomDocument();
                   oDomDoc = doParseXML(query); //this function just uses the Sarissa library to parse the String into a DOM Document (this bit works)
                  
                   var newparam = oDomDoc.createElement("param");
                   newparam.setAttribute("type",type);
                  
                   //works in both IE and Mozilla, thankfully
                   new_text = oDomDoc.createTextNode(param);
                   newparam.appendChild(new_text);
                  
                   var request = oDomDoc.getElementsByTagName("request")[0];
                   request.appendChild(newparam);
                  
                   return oDomDoc;
            }

I really really dislike using JavaScript and the DOM.

Thanks to zeroreality - spend them wisely.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question