Solved

Urgent!  How do you parse thru XML files if you don't know the names of the nodes up front?

Posted on 2004-10-04
11
210 Views
Last Modified: 2013-12-24
Urgent!  How do you parse thru XML files if you don't know the names of the nodes up front?  I have a bunch of XML files I need to process.  I am using ColdFusion 4.5 for development with a CF 5.0 server.  (don't ask)  I need to grab the data and place into an Access database.  I can do this process for one file if I know the nodes.  I am unable to do the process if I don't know the nodes.  Please help. Time is running short.  Thanks for any help.

ptslv
0
Comment
Question by:ptslv
  • 6
  • 4
11 Comments
 
LVL 21

Expert Comment

by:pinaldave
ID: 12217809
Well, I do not remember much of 4.5 but following are the points you can look up google and follow up more...
# XML parsers (DOM vs. SAX)
# Web Distributed Data Exchange (WDDX) (serialization and deserialization, plus server-to-browser and server-to-server data exchange examples)
Regareds,
---Pinal
0
 

Author Comment

by:ptslv
ID: 12217852
Thanks, Pinal.  But I've been looking at those links for the past 2 weeks.  They don't help me.  I need a solid example that works.  I don't have time left for trial and error.

ptslv
0
 

Author Comment

by:ptslv
ID: 12217982
I have finally gotten the traversal thru the nodes to work using the code below.  Now how do I create the form field from the unknown node and place into the database?

<html>
<head>
<script type="text/javascript" for="window" event="onload">
      var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.load(<cfoutput>"#URL.yourFileName#"</cfoutput>);
      var doc=xmlDoc.documentElement;
    traverse(doc);

function traverse(tree)
{
      alert("at Traverse");
    if(tree.hasChildNodes())
      {
        document.write('<ul><li>');
        document.write('<b>'+tree.tagName+' : </b>');
        var nodes=tree.childNodes.length;
        for(var i=0; i<tree.childNodes.length; i++)
            traverse(tree.childNodes(i));
        document.write('</li></ul>');
    }
    else
        document.write(tree.text);
}
</script>
</head>

ptslv
0
 
LVL 4

Expert Comment

by:reggi635
ID: 12220631
So finally you decided to stick with parsing of XML with Javascript?
If you don't know the name of the nodes in your XML you need to spank that guy who gave you that inconsistent XML.  :)  Just Kidding.
I would use a try catch in javascript that is all I can think of right now.
Like this:

var doc=xmlDoc.documentElement;
try{
   traverse(doc);
} catch(objException) {
   alert("Damm It!");
}

But If I understand correctly you were uploading the XML file first and then reading it where did these form fields come into play??
What are you trying to do?

REGGI
0
 

Author Comment

by:ptslv
ID: 12220746
Hi, REGGI.

When I first started this 'little' project, I was expecting all received XML files to have the same structure (field names).  Alas, I found that each file may contain data for 1, 2, or more tables.  So, I need to develop a routine to select or enter a file, load it, and capture the table name, the fields, and the data.  Right now, I can get the file, load it, traverse it, and display the file.

The javascript was the only way I could get the traversal to work.  I can display the xml files all day.  I am now playing around with the WDDX approach.  But I still cannot figure out how to pull the 'unknown' names of the nodes and the data into a structure so that I can get the data to a database.  

ptslv
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:reggi635
ID: 12221781
I guess having couple of conditional statements inside your javascript function can help.
something like
if( tree.nodeName == "DocumentName") {
   // do all this ... I guess you had a node called DocumentName in ur XML.
}

Is your worry about, If there are 10 or 20 DocumentNames then what do I do?
OR
Not sure what nodename will I be given from the XML?

If you cannot know the name of the the node then let it be parsing on server side of Client the problem will be the same.

REGGI
0
 

Author Comment

by:ptslv
ID: 12225946
Actually, both the number of DocumentNames and which nodes are in them is the problem.  I think I have a solution though.  Figured it out as I was driving in to work this morning.  Since the user knows what she is looking for, I am going to let her choose which document type she's looking at and go it from there.  I will let you know if it works.

ptslv
0
 
LVL 4

Expert Comment

by:reggi635
ID: 12226950
Aaahaa Fresh day new ideas... This is how it works most of the time :P
One more idea just in case if you need it.
<script>
         var documentName = new Array();
         /* Loop thru the XML and append values in the array */
</script>
0
 

Author Comment

by:ptslv
ID: 12228468
OK, thanks.

Question - how can I assign an attribute to a variable?

I have:  XML:  <DocumentInfo documentType="LATH">

and I want to do this (DocumentType is a form field):

DocumentType.value = xmlDoc.getElementsBydocumentType("LATH").text;

p
0
 
LVL 4

Accepted Solution

by:
reggi635 earned 500 total points
ID: 12228825
Hmm dont really understand what you mean
Perhaps trying to do this???

function traverse(tree) {
      if(tree.hasChildNodes()) {
                var nodes=tree.childNodes.length;
           if(tree.nodeName == "DocumentType") {
            document.frm.DocumentType.value = tree.childNodes(i).text;
           }
        for(var i=0; i<tree.childNodes.length; i++) {
            traverse(tree.childNodes(i));
        }
    }
}
</script>

<form name="frm">
      <input type="Text" name="DocumentType" value="">
</form>
0
 

Author Comment

by:ptslv
ID: 12497144
Reggi -

Told you I'd let you know I was able to do this.  Ended up doing this in C#.  Followed your last comment to a degree.  I'm giving you the points.  Thanks for all the help.  FYI, I ended up created a class containing all the fields.  The following code is what I used to get the elements into a temp variable, and then place into the member fields.  Maybe someone else can use it in the future.
Thanks again,

ptslv
-----------------------

function start()
{
  string TransType = DocType.ToString();
  string myurl = "D:\\MyData\\XMLFiles\\" + filename.ToString();
  data.m_FileName = filename.ToString();

  XmlReader reader = new XmlTextReader(myurl);
   string tempfield;
   string tempdata;
                        
  while(reader.Read())
  {
      try
      {
         if(reader.NodeType == XmlNodeType.Element && reader.IsEmptyElement == false)
         {
        if(reader.LocalName.ToString() == "DocumentInfo") // looking for a specific attribute
        {
           tempfield = "documentType";
           tempdata = reader.GetAttribute("documentType");
        }
        else
        {
           tempfield = reader.LocalName.ToString();
           tempdata = reader.ReadString().ToString();
        }
         }
         else if(reader.NodeType == XmlNodeType.Element && reader.IsEmptyElement == true)
         {
        tempfield = (reader.LocalName.ToString());
        tempdata = "";
        setData(tempfield, tempdata);
         }
         else if(reader.NodeType == XmlNodeType.Text)
         {
         string theText = reader.ReadString();
         tempfield = reader.LocalName;
         tempdata = (theText.ToString());
         setData(tempfield, tempdata);
         }
         else if(reader.NodeType == XmlNodeType.Comment)
         {
         string theCmt = reader.ReadString();
         tempfield = reader.LocalName;
         tempdata = (theCmt.ToString());
         setData(tempfield, tempdata);
         }
         else if(reader.NodeType == XmlNodeType.Entity)
         {
                   string theEntity = reader.ReadString();
         tempfield = reader.LocalName;
         tempdata = (theEntity.ToString());
         setData(tempfield, tempdata);
         }
         else if(reader.NodeType == XmlNodeType.Attribute)  // don't know if node is attribute or actual element
         {
         string theAttribute = reader.ReadString();
         tempfield = reader.LocalName;
         tempdata = (theAttribute.ToString());
         setData(tempfield, tempdata);
         }//end if
     }
     catch(Exception ermsg)
     {
      MessageBox.Show("Please notify programmer of error in processData process: " + ermsg.ToString());
      this.Close();
     }
  }//end while
  addMyRow(DocType);   // function loads the data into the member variables and calls a function to insert into the database
}//end function
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now