Mart
(KiX Supporter)
2007-04-20 10:16 AM
Reading XML file returns nothing

Guy's,

I'm trying to read an XML file with the code below. Unfortunately I get no results. All variables are empty. We have the same code in a different script that also reads an XML file and it works great. The XML encoding on both files is the same. Anyone got some bright ideas on this?

 Code:
Break on
Call @scriptdir + "\functions\LoadXML().udf"
Call @scriptdir + "\functions\ReadXMLValue().udf"

$xml = LoadXml(@SCRIPTDIR + "\test.xml")

$id = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/id")
$name = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/name")
$gender = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/gender")
$age = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/age")
$place = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/place")
$province = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/province")
$date_membership = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/date_membership")
$url_profile = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/url_profile")
$url_photothumb = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/url_photothumb")
$advert_title = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/advert_title")
$advert_description = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/advert_description")

?$id
?$name
?$gender
?$age
?$place
?$province
?$date_membership
?$url_profile
?$url_photothumb
?$advert_title
?$advert_description
Sleep 10


LoadXML UDF:
 Code:
Function LoadXml($filename)

Dim $, $rootNode
$loadXml = CreateObject("Microsoft.XMLDOM");
If Not $loadXml
	Return
EndIf

$= $loadXml.Load($filename)

EndFunction


ReadXMLValue UDF:
 Code:
Function ReadXmlValue($xml, $key, optional $defaultValue)

Dim $sectionNode
$sectionNode = $xml.SelectSingleNode($key);
If Not $sectionNode
	$ReadXmlValue = $defaultValue
Else
	$ReadXmlValue = $sectionNode.FirstChild.Text;
EndIf

EndFunction


XML File:
 Quote:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<stuff>
<morestuff>
<id>34538</id>
<name>name</name>
<gender>Male</gender>
<age>29</age>
<place>Amsterdam</place>
<province>Noord Holland</province>
<date_membership>18-april-2007</date_membership>
<url_profile>http://somesite</url_profile>
<url_photothumb>http://somesite.nl/someimage.jpg</url_photothumb>
<advert_title>Title</advert_title>
<advert_description>Description</advert_description>
</morestuff>
</stuff>


Arend_
(MM club member)
2007-04-20 12:56 PM
Re: Reading XML file returns nothing

I did the code like this and works fine:
 Code:
$xml = LoadXml(@SCRIPTDIR + "\test.xml")

$id = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/id")
$name = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/name")
$gender = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/gender")
$age = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/age")
$place = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/place")
$province = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/province")
$date_membership = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/date_membership")
$url_profile = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/url_profile")
$url_photothumb = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/url_photothumb")
$advert_title = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/advert_title")
$advert_description = ReadXmlValue($xml, "stuff/morestuff" + $index2 + "/advert_description")

? $id
? $name
? $gender
? $age
? $place
? $province
? $date_membership
? $url_profile
? $url_photothumb
? $advert_title
? $advert_description

Function ReadXmlValue($xml, $key, optional $defaultValue)
  Dim $sectionNode
  $sectionNode = $xml.SelectSingleNode($key);
  If Not $sectionNode
    $ReadXmlValue = $defaultValue
  Else
    $ReadXmlValue = $sectionNode.FirstChild.Text;
  EndIf
EndFunction

Function LoadXml($filename)
  Dim $rootNode
  $loadXml = CreateObject("Microsoft.XMLDOM")
  If Not $loadXml
    Return
  EndIf
  $= $loadXml.Load($filename)
EndFunction


Mart
(KiX Supporter)
2007-04-20 02:13 PM
Re: Reading XML file returns nothing

Wierd

Copied your exact code and ran it. No luck, all vars are still empty. Doing a LEN() on them also shows a length of 0. Its extra weird because the exact same stuff runs on an other XML file and returns the values just fine.


Les
(KiX Master)
2007-04-20 02:20 PM
Re: Reading XML file returns nothing

Have you compared files? Maybe one is unicode?

Arend_
(MM club member)
2007-04-20 02:34 PM
Re: Reading XML file returns nothing

Very weird, for the record I write all my code in notepad using ANSI.

ShawnAdministrator
(KiX Supporter)
2007-04-20 02:47 PM
Re: Reading XML file returns nothing

Both scripts work for me.

Les
(KiX Master)
2007-04-20 02:49 PM
Re: Reading XML file returns nothing

 Originally Posted By: apronk
Very weird, for the record I write all my code in notepad using ANSI.
I meant the XML files.


Arend_
(MM club member)
2007-04-20 02:57 PM
Re: Reading XML file returns nothing

Should make sure "C:\WINDOWS\system32\msxml3.dll" is fully registered.
Also, like I said b4 make sure the XML file is in ANSI as well.


Mart
(KiX Supporter)
2007-04-20 03:00 PM
Re: Reading XML file returns nothing

Hmmm... Dunno. Will check.

Mart
(KiX Supporter)
2007-04-20 03:04 PM
Re: Reading XML file returns nothing

 Originally Posted By: apronk
Very weird, for the record I write all my code in notepad using ANSI.


Code is written in ASE. XML is supplied by the customer.


Arend_
(MM club member)
2007-04-20 03:36 PM
Re: Reading XML file returns nothing

Could you add some @error @serror checks in the UDF's ?

Mart
(KiX Supporter)
2007-04-20 03:37 PM
Re: Reading XML file returns nothing

 Originally Posted By: apronk
Should make sure "C:\WINDOWS\system32\msxml3.dll" is fully registered.
Also, like I said b4 make sure the XML file is in ANSI as well.


Yep, msxml3.dll is registered and I saved the file with a different name and as ANSI from notepad.
Still no luck. Will keep on searching.


Arend_
(MM club member)
2007-04-20 03:40 PM
Re: Reading XML file returns nothing

Could you install this: MSXML 6.0 Change it to Dutch if your OS is dutch.

Btw, seeying as you are from Rotterdam yourself imma forgive you for putting "Amsterdam" in the xml ;\)


Mart
(KiX Supporter)
2007-04-20 03:47 PM
Re: Reading XML file returns nothing

 Originally Posted By: apronk
Could you add some @error @serror checks in the UDF's ?


Allrightie then.
Found something I guess. Just dunno what to do with it \:\(
Changed the ReadXmlValue UDF and added some error checking.

 Code:
Function ReadXmlValue($xml, $key, optional $defaultValue)
	Dim $sectionNode
	$sectionNode = $xml.SelectSingleNode($key)
	If Not $sectionNode
		??@ERROR
		?@SERROR
		?"Not section node"
		Sleep 0.5
		$ReadXmlValue = $defaultValue
	Else
		??@ERROR
		?@SERROR
		?"Section node"
		Sleep 0.5
		$ReadXmlValue = $sectionNode.FirstChild.Text;
	EndIf
EndFunction


Returs this:
 Code:
-2147352573
Member not found.
Not section node


Mart
(KiX Supporter)
2007-04-20 03:55 PM
Re: Reading XML file returns nothing

 Originally Posted By: apronk
Could you install this: MSXML 6.0 Change it to Dutch if your OS is dutch.

Btw, seeying as you are from Rotterdam yourself imma forgive you for putting "Amsterdam" in the xml ;\)


A higher version is already installed on my system comes up when I try to install this.

BTW: You don’t want to know what is really in the XML file. I changed the data between the XML tags (not the tags itself) because they are erotic ads that we process for our customer and could/should get censored by the board.


Arend_
(MM club member)
2007-04-20 03:59 PM
Re: Reading XML file returns nothing

Could you try it on a different machine then ? I'm betting this has to do with your MSXML version. 6.0 is the latest provided by MS.

Mart
(KiX Supporter)
2007-04-20 04:12 PM
Re: Reading XML file returns nothing

Different system = same results.

I'll do some searching on the error code and see what comes up.


ShawnAdministrator
(KiX Supporter)
2007-04-20 04:15 PM
Re: Reading XML file returns nothing

Has anyone got both scripts to work ok ? FYI - I'm running Windows XP SP1 (out-of-date I know, I know) ...

Arend_
(MM club member)
2007-04-20 04:16 PM
Re: Reading XML file returns nothing

I'd really try to install MSXML 6.0 anyway. Did the different system have 6.0 ?

Arend_
(MM club member)
2007-04-20 04:17 PM
Re: Reading XML file returns nothing

 Originally Posted By: Shawn
Has anyone got both scripts to work ok ? FYI - I'm running Windows XP SP1 (out-of-date I know, I know) ...


Hey Shawn, yeah both work ok on my system.
I'm on SP2 with all updates installed.


Mart
(KiX Supporter)
2007-04-20 04:22 PM
Re: Reading XML file returns nothing

 Originally Posted By: apronk
I'd really try to install MSXML 6.0 anyway. Did the different system have 6.0 ?


Yeah 6.0 is on both systems. It just errors like I said above and then the installation stops. No option to continue.
The error I got from the script seems to translate to error 3 - path not found.

Getting sick of it or is it the chocolate I'm eating while testing and searching

BTW: Running WinXp Pro SP. Fully updated.


Arend_
(MM club member)
2007-04-20 04:40 PM
Re: Reading XML file returns nothing

Change the @scriptdir to a hard path. no UNC :P

Mart
(KiX Supporter)
2007-04-20 04:52 PM
Re: Reading XML file returns nothing

God d#mn
That did it \:o

I thought I had that at first. Guess not Will do some more testing and see if stuff keeps working.


Arend_
(MM club member)
2007-04-20 05:05 PM
Re: Reading XML file returns nothing

Hehe, at least it's solved now.