/    Sign up×
Community /Pin to ProfileBookmark

Changing the nodevalue of xmlelement from null

Hello all,

i had a problem with internet explorer, i want to store some user entered values to xml document. my problem is that when the user left one field empty first time,
and tried to change the value next time i am using the following code
to chenge it

nameElement.firstChild.nodeValue=nameObj.value;

it work well when the nodeValue is not empty both ie and gecko

but if the user change the null value of the textbox
it display error-nameElement.firstChild is null or not an object.

i tried changing the code to

nameElement.nodeValue=nameObj.value;

but it only works in gecko browsers.
if anybody know to solve the problem
please help me…

Regards….

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 23.2007 — Sounds confusing to me. What kind of element is [B]nameElement[/B]? A textbox? an XML tag? What [I]value[/I] do you intend to change? The [B]value[/B] attribute's value or the textNode wrapped by that element?
Copy linkTweet thisAlerts:
@sanojcpauthorAug 23.2007 — Here is the sample doc,

user entering an id->checking id in server->returning xml.

if id exists xml is(ie. 0001)

<data>

<userid>0001</userid>

<name>testing user</name>

<dept>TEST</dept>

</data>

if id not exists xml will be,

<data>

<userid>0001</userid>

<name />

<dept />

</data>

the nameElement is

[B]nameElement=xmldoc.getElementsByTagName('name')[0];[/B]

[B]nameObj is a textbox to enter user name[/B]

the problem is in the second part if the user id not exists then

the user need to enter his/her name. So that i want to change the

nameElement's nodeValue. and i tried,

nameElement.firstChild.nodeValue=nameObj.value;

this cause the error 'firstChild' is null or not an object

so i tried,

nameElement.nodeValue=nameObj.value;

it worked well in gecko browsers, but IE said

This operation cannot be performed with a Node of type ELEMENT


Hope you got me, now what i do?

Thanks for reply

Regards....
Copy linkTweet thisAlerts:
@KorAug 23.2007 — well, yes. If a textNode is empty, IE considers it null as child.

On the other hand, if the value is null, your XML should look like:
<i>
</i>&lt;data&gt;
&lt;userid&gt;0001&lt;/userid&gt;
&lt;name&gt;&lt;/name&gt;
&lt;dept&gt;&lt;/dept&gt;
&lt;/data&gt;


As the closing tags notation <tag /> is used only in XHTML for the HTML so called non-empty tags (INPUT, IMG, BR...)


If so, try this:
<i>
</i>if(nameElement.firstChild){
nameElement.firstChild.nodeValue=nameObj.value;
}
else{
nameElement.appendChild(xmldoc.document.createTextNode(nameObj.value))
}
Copy linkTweet thisAlerts:
@sanojcpauthorAug 23.2007 — Hmmm, i tried it but

it's not working and saying that xmldoc.document is null or not an object.

here is the code that am using to create XMLDomDocument, i am passing the

XMLHttpRequest's responseText to this function(i mean [B]xText[/B]),

function getXDocument([B]xText[/B]){

var xmlDocument=null;

if (window.ActiveXObject){

xmlDocument = [B]createMsxml2DOMDocumentObject[/B]();

xmlDocument.async="false";

xmlDocument.loadXML(xText);

}else if (document.implementation && document.implementation.createDocument){

var parser=new DOMParser();

xmlDocument=parser.parseFromString(xText,"text/xml");

}

return xmlDocument;

}





the [B]createMsxml2DOMDocumentObject[/B]() function is just checking the version

of msxml and choose the best one.



is there any problem in this?



This is the first project i am doing using xml...



Thanks for your help,



Regards....
Copy linkTweet thisAlerts:
@sanojcpauthorAug 24.2007 — But the following code working well

nameElement.appendChild(xmldoc.documentElement.ownerDocument.createTextNode(nameObj.value)) ;

is there any problem if i used the above code??

Regards...
Copy linkTweet thisAlerts:
@KorAug 24.2007 — it's ok
Copy linkTweet thisAlerts:
@sanojcpauthorAug 24.2007 — Thanks...
×

Success!

Help @sanojcp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...