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
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

 
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
 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Domino Load Balancer For 2048 Key Requirement 7 53
retrieving files from old server once DNS has changed 10 72
JKS to store upstart data 2 119
Website URL redirection 10 81
This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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