Archived Forum Post

Index of archived forum posts


Problem navigating xml data in Chilkat_9_5_0.Xml

Aug 04 '14 at 21:14


I'm attempting to use Chilkat_9_5_0.Xml. Here is a test xml document that I'm using to test Chilkat XML methods, and it's giving me unexpected results. (This is a shortened version of an XML file returned by Chilkat Ftp2)

Basically, I'm looking for file name, file size, and path information. The xml file has some <file> nodes at the root, and some under two levels of <dir> nodes, and I'm having trouble figuring out how to navigate the xml file so that I can download files I'm missing into the same directory structure. (I don't want all of them, just files that don't exist or that are the wrong size, so I can't just download the whole thing.)

<?xml version="1.0" encoding="utf-8" ?>
    <file sz="34731" dt="Sun, 27 Jul 2014 05:00:00 -0400">140725AM.lmt</file>
    <file sz="24719" dt="Mon, 28 Jul 2014 05:00:00 -0400">140725CN.lmt</file>
    <dir name="20140727">
        <dir name="Update_1">
            <file sz="34731" dt="Sun, 27 Jul 2014 05:00:00 -0400">140725AM.lmt</file>
            <file sz="237576" dt="Sun, 27 Jul 2014 05:00:00 -0400">140727LS.lmt</file>
            <file sz="225952" dt="Sun, 27 Jul 2014 05:00:00 -0400">140727PD.lmt</file>
    <dir name="20140729">
        <dir name="Update_1">
            <file sz="44585" dt="Tue, 29 Jul 2014 05:00:00 -0400">140728AM.lmt</file>
            <file sz="24405" dt="Tue, 29 Jul 2014 05:00:00 -0400">140728CN.lmt</file>
            <file sz="134287" dt="Tue, 29 Jul 2014 05:00:00 -0400">140728WH.lm</file>
    <dir name="20140728">
        <dir name="Update_1">
            <file sz="24719" dt="Mon, 28 Jul 2014 05:00:00 -0400">140725CN.lmt</file>
            <file sz="25025" dt="Mon, 28 Jul 2014 05:00:00 -0400">140726CN.lmt</file>
            <file sz="25115" dt="Mon, 28 Jul 2014 05:00:00 -0400">140727CN.lmt</file>
            <file sz="152048" dt="Mon, 28 Jul 2014 05:00:00 -0400">140728AN.lmt</file>
            <file sz="138385" dt="Mon, 28 Jul 2014 05:00:00 -0400">140728ET.lmt</file>
            <file sz="163350" dt="Mon, 28 Jul 2014 05:00:00 -0400">140728GC.lmt</file>
        <dir name="Update_2">
            <file sz="83054" dt="Wed, 30 Jul 2014 05:15:00 -0400">140730HH.lmt</file>

Here is some test code that gives peculiar results:

   strXml = WScript.Arguments.Item(0)
    set FileTreeXml = CreateObject("Chilkat_9_5_0.Xml")
    success = FileTreeXml.LoadXmlFile(strXml)
'If there is a directory, it's at the root, and I should be able to get 
'its name here, no navigating.
sDir = FileTreeXml.GetAttrValue("name")
wscript.echo "* sDir=" & sDir

'stepping through children 'The first child is a directory, and I can get its name by navigating to the first child

success = FileTreeXml.FirstChild2()
sSubDir = FileTreeXml.GetAttrValue("name")
wscript.echo "** sSubDir=" & sSubDir

'note also that the files are the children of the last directory to have a name, so there is no need to navigate further

sName = FileTreeXml.GetChildContent("file")
wscript.echo "*** sName=" & sName

'useful strategy for getting all the files at this node...
sName = FileTreeXml.GetChildContentByIndex(0)      'etc...
wscript.echo "**** sName=" & sName

Here are the results:

    • sDir=20140728
  1. ** sSubDir=Update_1
  2. *** sName=140725CN.lmt
  3. **** sName=140725CN.lmt

And here is my problem: The first <dir> node doesn't have the name 20140728 (That's the name of the third "dir" node.) And the other results all apply to that 3rd <dir> node. I like the conistancy, but I don't know why my pointer has ended up there.

Can anyone explain why this is happening? And if anyone has ideas about the best way to get the "file" node and path info when my xml file has those nodes at two different levels, I'd be grateful to hear them.

Thank you to all. Judith

Accepted Answer

The XML appears to be malformed - there is an extra </file> closing tag near the top of the <dirTree> content.

After fixing that, you should be able to traverse the tree properly.