Solved

Manipulating XML with the DOM

Posted on 2006-07-17
5
321 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

737 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