Changing attribute values in xml with javascript

Giganews Newsgroups
Subject: Changing attribute values in xml with javascript
Posted by:  CueneydErb…@googlemail.com
Date: Fri, 21 Mar 2008

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=3D"1.0" encoding=3D"iso-8859-1" ?>
<KdStamm>
<customer id=3D"001" address=3D"hannover" lon=3D"" lat=3D"" info=3D"test"></=

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

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

    customers =3D xml_doc.getElementsByTagName("customer");

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

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

                lon =3D gLon;//global variable with longitude coordinate
                lat =3D 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=FCneyd

Replies