Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
222 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
Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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