Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Manipulating XML with the DOM

Posted on 2006-07-17
5
Medium Priority
?
330 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

618 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