본문 바로가기

IT/xml

[XML] createElement,setNamedItem,appendChild

반응형
<script language="javascript">
xmldoc = new ActiveXObject("Msxml2.DOMDocument");
    xmldoc.async =false;
    var root,name,att_f,att_l;
    var names = [["기수","김"],["길동","홍"],["현아","신"],["나라","장"]]
   
    var root = xmldoc.createElement("simple");
   
    for(var i=0; i <names.length; i++)
    {
        name = xmldoc.createElement("name");
        att_f = xmldoc.createAttribute("first");att_f.nodeValue = names[i][0];
        att_l = xmldoc.createAttribute("last");att_l.nodeValue = names[i][1];
        name.attributes.setNamedItem(att_f);
        name.attributes.setNamedItem(att_l);
        root.appendChild(name);
    }
    xmldoc.documentElement = root;
    alert(xmldoc.xml);

</script>