Solved

Manipulating XML with the DOM

Posted on 2006-07-17
5
315 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
or even:
newparam.appendChild(oDomDoc.createTextNode(param));
0
 

Author Comment

by:mb2297
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now