Changing attribute values in xml with javascript

C

CueneydErbay

My question is how I can change a value of an attribute in an xml file
I loaded before.
I tried the method setAttribute("attribute name", "attribute value")
but it doesn't work.

That's my sample xml file:
<?xml version="1.0" encoding="iso-8859-1" ?>
<KdStamm>
<customer id="001" address="hannover" lon="" lat="" info="test"></
customer>
<customer id="002" address="munich" lon="" lat="" info="test"></
customer>
<customer id="003" address="hamburg" lon="" lat="" info="test"></
customer>
</KdStamm>

and that's an extract of my code (I loaded the xml file already
before):

customers = xml_doc.getElementsByTagName("customer");

// iterate through all nodes ( customer )
for(i = 0; i < customers.length; i++)
{
if (customers.nodeType==1)
{
CustomersItem = customers.item(i);
var lon = CustomersItem.getAttribute("lon");
var lat = CustomersItem.getAttribute("lat");
var htmlContent = CustomersItem.getAttribute("info");

// if lon & lat == "" then get coordinates out of the
address
if ( lon == "" & lat =="")
{
var XMLaddress =
CustomersItem.getAttribute("address");
geocode(XMLaddress);

lon = gLon;//global variable with longitude coordinate
lat = gLat; // global variable with latitude
coordinate
alert(gLon + " " + gLat);
}
// change attributes lon and lat out of node, problem
starts with these lines
CustomersItem.setAttribute("lon", lon);
CustomersItem.setAttribute("lat", lat);
}

I don't know why but I can't change the values of the attributes.

Thanks in advance.

Cüneyd
 
M

Martin Honnen

<KdStamm>
<customer id="001" address="hannover" lon="" lat="" info="test"></
customer>
<customer id="002" address="munich" lon="" lat="" info="test"></
customer>
<customer id="003" address="hamburg" lon="" lat="" info="test"></
customer>
</KdStamm>

and that's an extract of my code (I loaded the xml file already
before):

customers = xml_doc.getElementsByTagName("customer");

// iterate through all nodes ( customer )
for(i = 0; i < customers.length; i++)
{
if (customers.nodeType==1)
{
CustomersItem = customers.item(i);


I don't understand why you use get_Elements_ByTagName but then check
nodeType == 1, that does not make sense, you get an element node in any
case. And using customers first to then later use customers.item(i)
does not make sense either, it suffices to se
var customerItem = customers;

// change attributes lon and lat out of node, problem
starts with these lines
CustomersItem.setAttribute("lon", lon);
CustomersItem.setAttribute("lat", lat);
}

I don't know why but I can't change the values of the attributes.

Do you get any error message calling setAttribute? Those two lines
should work but you are simply changing the attribute values of the
in-memory XML DOM document, you are not saving those changes anywhere.
How exactly do you check that the setAttribute() call worked?
 
T

Thomas 'PointedEars' Lahn

My question is how I can change a value of an attribute in an xml file
I loaded before.
I tried the method setAttribute("attribute name", "attribute value")
but it doesn't work.

Yes, it does.
That's my sample xml file:
<?xml version="1.0" encoding="iso-8859-1" ?>
<KdStamm>
<customer id="001" address="hannover" lon="" lat="" info="test"></
customer>

<customer id="001" address="hannover" lon="" lat="" info="test" />

is probably easier to maintain. In applications of XML, you SHOULD use the
XML SHORTTAG syntax for elements with EMPTY content model, and you MAY use
it for any element that has no content.

http://www.w3.org/TR/xml/#IDAK0FS
<customer id="002" address="munich" lon="" lat="" info="test"></
customer>
<customer id="003" address="hamburg" lon="" lat="" info="test"></
customer>
</KdStamm>

and that's an extract of my code (I loaded the xml file already
before):

customers = xml_doc.getElementsByTagName("customer");
^^^^^^^^[1]

Are you declaring `customers' somewhere?
// iterate through all nodes ( customer )
for(i = 0; i < customers.length; i++)

Harder to maintain (not enough Pretty Printing), inefficient and potentially
error-prone (are you declaring `i' somewhere?). Consider instead:

for (var i = 0, len = customers.length; i < len; i++)

If reverse order iteration is acceptable:

for (var i = customers.length; i--;)
{
if (customers.nodeType==1)

^^^^^^^^^^^[2]

Superfluous. `customers' is a reference to a DOM2Core:NodeList of element
nodes.[^1] An element node will always have nodeType == 1 ==
DOM2Core:Node::ELEMENT_NODE. It does not miraculously become a text node,
for example.
{
CustomersItem = customers.item(i);

Why do you call item() here without feature test? You have successfully
used the bracket property accessor before.[^2]

Identifiers starting with a capital letter should be reserved for
constructors. BTW, are you declaring `CustomersItem' somewhere?
[...]
if ( lon == "" & lat =="")
^
`&' is the *binary* AND operator; while that should work here (because `=='
takes precedence over `&', `true' evaluates to 1, and `false' to 0), you
should use `&&', the logical AND operator, instead. Shortcut evaluation of
logical operators prevents `lat == ""' from being evaluated if `!(lon == "")'.

That said,

if (!(lon && lat))

is equivalent.
{
[...]
alert(gLon + " " + gLat);

alert() is a method of Window objects and should be called so:

window.alert(...);
}
[...]
}

I don't know why but I can't change the values of the attributes.

Please debug before you post next time.

http://jibbering.com/faq/#FAQ4_43


PointedEars
 
C

CueneydErbay

<KdStamm>
<customer id="001" address="hannover" lon="" lat="" info="test"></
customer>
<customer id="002" address="munich" lon="" lat="" info="test"></
customer>
<customer id="003" address="hamburg" lon="" lat="" info="test"></
customer>
</KdStamm>
and that's an extract of my code (I loaded the xml file already
before):
customers = xml_doc.getElementsByTagName("customer");
// iterate through all nodes ( customer )
for(i = 0; i < customers.length; i++)
{
if (customers.nodeType==1)
{
CustomersItem = customers.item(i);


I don't understand why you use get_Elements_ByTagName but then check
nodeType == 1, that does not make sense, you get an element node in any
case. And using customers first to then later use customers.item(i)
does not make sense either, it suffices to se
var customerItem = customers;
// change attributes lon and lat out of node, problem
starts with these lines
CustomersItem.setAttribute("lon", lon);
CustomersItem.setAttribute("lat", lat);
}
I don't know why but I can't change the values of the attributes.

Do you get any error message calling setAttribute? Those two lines
should work but you are simply changing the attribute values of the
in-memory XML DOM document, you are not saving those changes anywhere.
How exactly do you check that the setAttribute() call worked?


OK, I changed that senseless part. I also debugged it but I got no
error message. Well, it may sound stupid but the reason for the
problem
can be or is that I didn't save the XML file. Due to the reason that
I'm quite new to javascript I don't really know how to do that ??

Cüneyd
 
M

Martin Honnen

OK, I changed that senseless part. I also debugged it but I got no
error message. Well, it may sound stupid but the reason for the
problem
can be or is that I didn't save the XML file. Due to the reason that
I'm quite new to javascript I don't really know how to do that ??

With client-side JavaScript you usually load an XML document from a HTTP
server so to save the data back on the server you need to make HTTP POST
request posting the XML to the server, then server-side scripting needs
to take care of saving the received XML on the server's file system.
Thus if you want to save the XML you modify in the browser back to the
server then you need to make HTTP POST request
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'save.asp', true);
httpRequest.send(xml_doc);
and save.asp on the server takes care of saving the received XML to the
file system.
 
C

CueneydErbay

With client-side JavaScript you usually load an XML document from a HTTP
server so to save the data back on the server you need to make HTTP POST
request posting the XML to the server, then server-side scripting needs
to take care of saving the received XML on the server's file system.
Thus if you want to save the XML you modify in the browser back to the
server then you need to make HTTP POST request
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'save.asp', true);
httpRequest.send(xml_doc);
and save.asp on the server takes care of saving the received XML to the
file system.

Thanks for the reply but I'm using an ASP.NET server and it's not
working there.
Is there any other solution for ASP.NET?

Cüneyd
 
P

pr

That should be 'POST' instead of 'GET'.

Please don't quote signatures.
Thanks for the reply but I'm using an ASP.NET server and it's not
working there.
Is there any other solution for ASP.NET?

You haven't said what isn't working and "not working" isn't a
sufficiently detailed description of an error message or unexpected outcome.

You can test whether setAttribute() works as expected with:

el.setAttribute("myAttr", "100");
window.alert(el.getAttribute("myAttr")); // '100'

As to saving a modified XML document, neither posting data using the
XMLHttpRequest object nor saving posted data to the file system in
ASP.NET is a particularly obscure subject, so you should find plenty on
the web. Only the former is on-topic in a JavaScript newsgroup. Good
places to start include <URL:
http://jibbering.com/2002/4/httprequest.html> (the section entitled
'Using XMLHTTP with GOOGLE's SOAP API' is pretty much what you want) and
MSDN <URL: http://msdn.microsoft.com>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top