Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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
?
228 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

581 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