Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 13th, 2005, 05:14 AM
Registered User
 
Join Date: Jan 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default XMLDOM problem

I want to achieve the below structure using javascript.


<?xml version="1.0" encoding="UTF-8"?>
<root>
    <forms id="180" name="form name">
        <structure>
            <CID_541_Row_503/>
            <CID_542_Row_503/>
            <CID_543_Row_503/>
            <CID_544_Row_503/>
            <CID_545_Row_503/>
            <CID_541_Row_504/>
            <CID_542_Row_504/>
            <CID_543_Row_504/>
            <CID_544_Row_504/>
            <CID_545_Row_504/>
            <CID_541_Row_505/>
            <CID_542_Row_505/>
            <CID_543_Row_505/>
            <CID_544_Row_505/>
            <CID_545_Row_505/>
        </structure>

        <data>
                      <formData>
                             <CID_541_Row_503>0</CID_541_Row_503>
                             <CID_542_Row_503>1</CID_542_Row_503><
                             <CID_543_Row_503>2</CID_543_Row_503>
                             <CID_544_Row_503>3</CID_544_Row_503>
                             <CID_545_Row_503>4</CID_545_Row_503>
                             <CID_541_Row_504>5</CID_541_Row_504>
                             <CID_542_Row_504>6</CID_542_Row_504>
                             <CID_543_Row_504>7</CID_543_Row_504>
                             <CID_544_Row_504>8</CID_544_Row_504>
                             <CID_545_Row_504>9</CID_545_Row_504>
                             <CID_541_Row_505>10</CID_541_Row_505>
                             <CID_542_Row_505>11</CID_542_Row_505>
                             <CID_543_Row_505>12</CID_543_Row_505>
                             <CID_544_Row_505>13</CID_544_Row_505>
                             <CID_545_Row_505>14</CID_545_Row_505>
                       </formData>

               <formData>
                             <CID_541_Row_503>0</CID_541_Row_503>
                             <CID_542_Row_503>1</CID_542_Row_503><
                             <CID_543_Row_503>2</CID_543_Row_503>
                             <CID_544_Row_503>3</CID_544_Row_503>
                             <CID_545_Row_503>4</CID_545_Row_503>
                             <CID_541_Row_504>5</CID_541_Row_504>
                             <CID_542_Row_504>6</CID_542_Row_504>
                             <CID_543_Row_504>7</CID_543_Row_504>
                             <CID_544_Row_504>8</CID_544_Row_504>
                             <CID_545_Row_504>9</CID_545_Row_504>
                             <CID_541_Row_505>10</CID_541_Row_505>
                             <CID_542_Row_505>11</CID_542_Row_505>
                             <CID_543_Row_505>12</CID_543_Row_505>
                             <CID_544_Row_505>13</CID_544_Row_505>
                             <CID_545_Row_505>14</CID_545_Row_505>
                       </formData>

               </data>
</forms>
</root>


But what i m getting right now is the structure below.

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <forms id="180" name="form name">
        <structure>
            <CID_541_Row_503/>
            <CID_542_Row_503/>
            <CID_543_Row_503/>
            <CID_544_Row_503/>
            <CID_545_Row_503/>
            <CID_541_Row_504/>
            <CID_542_Row_504/>
            <CID_543_Row_504/>
            <CID_544_Row_504/>
            <CID_545_Row_504/>
            <CID_541_Row_505/>
            <CID_542_Row_505/>
            <CID_543_Row_505/>
            <CID_544_Row_505/>
            <CID_545_Row_505/>
        </structure>

        <data>
                      <formData>
                             <CID_541_Row_503>0</CID_541_Row_503>
                             <CID_542_Row_503>1</CID_542_Row_503><
                             <CID_543_Row_503>2</CID_543_Row_503>
                             <CID_544_Row_503>3</CID_544_Row_503>
                             <CID_545_Row_503>4</CID_545_Row_503>
                             <CID_541_Row_504>5</CID_541_Row_504>
                             <CID_542_Row_504>6</CID_542_Row_504>
                             <CID_543_Row_504>7</CID_543_Row_504>
                             <CID_544_Row_504>8</CID_544_Row_504>
                             <CID_545_Row_504>9</CID_545_Row_504>
                             <CID_541_Row_505>10</CID_541_Row_505>
                             <CID_542_Row_505>11</CID_542_Row_505>
                             <CID_543_Row_505>12</CID_543_Row_505>
                             <CID_544_Row_505>13</CID_544_Row_505>
                             <CID_545_Row_505>14</CID_545_Row_505>
                       </formData>
        </data>

        <data>
               <formData>
                             <CID_541_Row_503>0</CID_541_Row_503>
                             <CID_542_Row_503>1</CID_542_Row_503><
                             <CID_543_Row_503>2</CID_543_Row_503>
                             <CID_544_Row_503>3</CID_544_Row_503>
                             <CID_545_Row_503>4</CID_545_Row_503>
                             <CID_541_Row_504>5</CID_541_Row_504>
                             <CID_542_Row_504>6</CID_542_Row_504>
                             <CID_543_Row_504>7</CID_543_Row_504>
                             <CID_544_Row_504>8</CID_544_Row_504>
                             <CID_545_Row_504>9</CID_545_Row_504>
                             <CID_541_Row_505>10</CID_541_Row_505>
                             <CID_542_Row_505>11</CID_542_Row_505>
                             <CID_543_Row_505>12</CID_543_Row_505>
                             <CID_544_Row_505>13</CID_544_Row_505>
                             <CID_545_Row_505>14</CID_545_Row_505>
                       </formData>

               </data>
</forms>
</root>


The code i m using to achieve the above structure is as follows,

<script language="JavaScript">
function genofflineXML()
{
                    xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
                    xmlDoc.async = false
                    xmlDoc.load("xml.xml")
                    alert(xmlDoc.xml)
                    var secChildArr = new Array()
                    nodeList = xmlDoc.getElementsByTagName("Root")
                    rootNode = nodeList(0)

                    if (rootNode.hasChildNodes)
                      {
                         firstChild = rootNode.firstChild

                             if (firstChild.hasChildNodes)
                              {
                                  secondChild = firstChild.firstChild

                                   if(secondChild.hasChildNodes)
                                    {
                                          secondChildLen = secondChild.childNodes.length


                                        for(i=0;i<secondChildLen;i++)
                                        {
                                          secChildArr[i] = secondChild.childNodes[i].nodeName
                                         }
                                      }

                                }

                       }


                           if(nodeList.length>0)
                               {
                                   var dataNode = xmlDoc.createElement("data")
                                   var formDataNode = xmlDoc.createElement("formData")

                                   var secArrLen = secChildArr.length

                                   for(k=0;k<secArrLen;k++)
                                    {
                                          var dataNodeNames = secChildArr[k] + "node"
                                       var dataNodeText = k

                                       dataNodeNames = xmlDoc.createElement(secChildArr[k])
                                     dataNodeNames.text = dataNodeText

                                     formDataNode.appendChild(dataNodeNames)

                                       var filePath = "xml.xml"



                                  }




                                  firstChildList = xmlDoc.getElementsByTagName("forms")

                                  if(firstChildList.length>0)
                                  {
                                        firstChild.appendChild(dataNode)
                                        dataNodeList = xmlDoc.getElementsByTagName("data")
                                      dataNode.appendChild(formDataNode)
                                  }




                           }

                                xmlDoc.save(filePath)
}
</script>


If possible please lemme know the glitch. I am not able to set a condition where the childnode can be appended to the data node.
 
Old January 13th, 2005, 05:19 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

It's hard to tell without your source XML but it could be here:
var dataNode = xmlDoc.createElement("data")
Does this already exist in your source file?

If so, you need to get this to point to the existing 'data' node, not create a new one. Perhaps:
var dataNode = xmlDoc.getElementsByTagName("data").Item(1)


 
Old January 16th, 2005, 01:38 PM
Registered User
 
Join Date: Jan 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <forms id="180" name="form name">
        <structure>
            <CID_541_Row_503/>
            <CID_542_Row_503/>
            <CID_543_Row_503/>
            <CID_544_Row_503/>
            <CID_545_Row_503/>
            <CID_541_Row_504/>
            <CID_542_Row_504/>
            <CID_543_Row_504/>
            <CID_544_Row_504/>
            <CID_545_Row_504/>
            <CID_541_Row_505/>
            <CID_542_Row_505/>
            <CID_543_Row_505/>
            <CID_544_Row_505/>
            <CID_545_Row_505/>
        </structure>
     </forms>
</root>


Well the above is my xml source. i also have posted it but the one already posted is what i want to achieve after modifyin the above structure. The data node has to be created once and then i keep adding child nodes (formData) to it as i have depicted in the second structure posted in my previous post. please refer to the second structure. thanx for reading and tryin to help me.

regards,

Wilbur





Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft.XMLDOM Alternative? gabster XSLT 4 September 11th, 2007 02:30 AM
JavaScript and XMLDOM kirthi97 XML 4 June 27th, 2006 02:22 AM
Pogramming the XmlDOM in C# sencee C# 5 March 10th, 2006 11:09 AM
access sub nodes using XMLDOM lian_a Classic ASP XML 2 January 19th, 2005 09:43 PM
XMLDOM problem braindog_43 Javascript How-To 2 January 16th, 2005 01:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.