Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To 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, 03:17 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, 04:59 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Are you trying to convert the first xml to the second? Life would be much simpler with an XSLT transformation.

Another point is that in general the style of elements you are using is very difficult to process, you would be better off using attributes, change this:
Code:
<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>
to something like:
Code:
<formData>
  <item CID="541" Row="503">0</item>
  <item CID="542" Row="503">1</item>
  <item CID="543" Row="503">2</item>
I'd pick something more meaningful than item but I don't know what you are trying to represent :)


--

Joe (Microsoft MVP - XML)
 
Old January 16th, 2005, 01:54 PM
Registered User
 
Join Date: Jan 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
  Well thanx for trying to help me out. But i guess i have not been able to express or document my problem very well, so i would like to apologize for that.

<root>
 <forms>
  <structure>
   <Col541_Row_543/>
  </structure>
 </forms>
</root>

please refer to structure one of my prev. post. this is what i have got as my source and what i load when loading my xml data.

<root>
 <forms>
  <structure>
   <Col541_Row_543/>
  </structure>

  <data>
    <formData id="1">
      <Col541_Row_543/>acd</Col541_Row_543>
    </formData>



    <formData id="2">
      <Col541_Row_543/>acd</Col541_Row_543>
    </formData>

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


Please refer to my structure one in the prev. post as to see what i am getting. But i want to achieve the above structure and i have also posted my Javascript code.
Once loaded i need to do the following









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 XML 2 January 16th, 2005 01:38 PM





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