Solved

Manipulating XML with the DOM

Posted on 2006-07-17
5
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

690 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