Link to home
Start Free TrialLog in
Avatar of Hojoformo
Hojoformo

asked on

Creating a New XML Node using Classic ASP/XMLDOM.

Need a little assistance creating a new XML node.  I need to create a new node called "zipCode" right under the existing node "UtilityId".    For example, I want my xml to look like:
  <UtilityId ID="1829118006" />
      <zipCode>12345</zipCode>

This is how I am currently doing it but I now this is not right.  
Set oXML = Server.CreateObject("Microsoft.XMLDOM")
Set oXMLRec = oXML.createElement("UtilityId")  
oXMLRec.setAttribute "ID", myaccount
oXMLRec.setAttribute "zipCode", myzip
oXML.documentElement.appendChild(oXMLRec)

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <BillingData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://apogee.net/WebService">
  <UtilityId ID="2222570016" zipCode="30907" />   ???? zipcode needs to be it's own node.  
  <billingCycleData />
  </BillingData>
ASKER CERTIFIED SOLUTION
Avatar of steveberzins
steveberzins

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dim objNode, objDOM

      'Create an XML DOM object from the XML file
      Set objDOM  = CreateObject("Microsoft.XMLDOM")            
      objDOM.async = false
      objDOM.load("yourFile.xml")

      'Display the original XML values
      txtOrig.value = objDOM.xml
      
      'Create a new Node object
      Set objNode = objDOM.getElementsByTagName("UtilityId")

      'Add the objects to the newly created Node
      AddNodeAttribute objDOM, objNode, "zipCode", "12345"

        'If you want to add another one then uncomment below line
      'AddNodeAttribute objDOM, objNode, "nodeName", "valueForNode"

      response.write objDOM.xml
Using Javascript you can do the same as below

<script language="javascript">

xmlDoc=loadXMLDoc("books.xml");

var x=xmlDoc.getElementsByTagName('UtilityId');
var newatt;

for (i=0;i<x.length;i++)
  {
  newatt=xmlDoc.createAttribute("zipCode");
  newatt.value="12345";
  x[i].setAttributeNode(newatt);
  }

</script>
Forced accept.

Computer101
EE Admin