Link to home
Start Free TrialLog in
Avatar of ptslv
ptslvFlag for United States of America

asked on

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

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
Avatar of pinaldave
pinaldave
Flag of India image

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
Avatar of ptslv

ASKER

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
Avatar of ptslv

ASKER

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
Avatar of reggi635
reggi635

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
Avatar of ptslv

ASKER

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
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
Avatar of ptslv

ASKER

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
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>
Avatar of ptslv

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of reggi635
reggi635

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptslv

ASKER

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