Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to assign a value to xml node

Status
Not open for further replies.

PleaseHelp1

Technical User
Nov 13, 2006
6
CA
I need to modify a xml document...

Using Javascript, I have loaded a xml file:

xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = SyncType;
xmlDoc.load(xmlfile.xml);

Lets say the xmlfile looks like this:
<?xml version="1.0" ?>
<Service>
<SvcId/>
<SvcName>Service Name</SvcName>
</Service>

I can change the service name to "whatever" by:
xmlDoct.getElementsByTagName("SvcName")[0].firstChild.nodeValue = "whatever";

but I CAN NOT add any value to SvcId since xmlDoct.getElementsByTagName("SvcId")[0].firstChild does not exist.

Your help is really appreciated.
Thanks,
J





 
Try using createNode:

Code:
var idNode = xmlDoct.getElementsByTagName('SvcId')[0];
idNode.appendChild(xmlDoct.createTextNode('someValueHere'));

Note sure if "createTextNode" is a method of "xmlDoct" or not, given that you've defined "xmlDoc" but not "xmlDoc[!]t[/!]"... but give it a whirl and see what happens.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thank you for the info and sorry about the typo. it is xmlDoc not xmlDoct and I have tried the code that you provided and it gives me error "Object does not support this property or method". The code works with document.createTextNode('some text here'), but it does not work with XMLDOM object.

Thanks again for your reply.
J
 
[tt]xmlDoc.getElementsByTagName("SvcId")[0].text="abc";[/tt]
 
>but I CAN NOT add any value to SvcId since xmlDoct.getElementsByTagName("SvcId")[0].firstChild does not exist.
Well, if you want to use createTextNode method etc and failed, that's fine. But what is said as a rational explanation is incorrect and self-misleading. This is how to do it even with null firstChild.
[tt]
var onode=xmlDoc.createTextNode("abc");
var onode_parent=xmlDoc.getElementsByTagName("SvcId")[0];
onode_parent.insertBefore(onode,onode_parent.firstChild);
[/tt]
 
tsuji,

Thanks for your first post "xmlDoc.getElementsByTagName("SvcId")[0].text="abc";" it works. However, on the second post, there is no misleading. Try it in your IE or Mozzila browser. You can create a text node using document.createTextNode but you ca not use your loaded xml document to create a text node. I have tried it in both browsers and both complained.

Thanks again,
J
 
BillyRayPreachersSon,

If I use document.createCreateTextNode('abc') and try to append it to my xmlDoc, it will generate an error "Type mismatch".

Thanks,
J
 
>Try it in your IE or Mozzila browser. You can create a text node using document.createTextNode but you ca not use your loaded xml document to create a text node
If the distiller is to produce whisky, you cannot use an olive oil press to do at its place.
 
tsuji,

FYI.

xmlDoc.getElementsByTagName("SvcId")[0].text

works for IE but not Mozzila.

Would you know what is the majic to make it work for Mozzila?

J
 
Here is the solution to my problem in both Mozzila and IE with the help of tsuji and BillyRayPreachersSon ....

In IE simply use:
xmlDoc.getElementsByTagName("SvcId")[0].text="some text here";

In Mozzila:
1- create a text node using document object
var TextNode = document.createTextNode('some text here')
2- Append it to the xml
xmlDocResult.getElementsByTagName("SvcId")[0].appendChild(TextNode)

Thanks again for your help.
J


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top