Page 1 of 1 1
Topic Options
#160021 - 2006-03-30 10:33 AM Reading XML formatted files
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Guy's,

If have an issues when reading an XML formatted file.
There is one part of the file that looks like the example given below. More data is above and below this example but that is all being processed like it should. This is the only part that gives me a hard time reading it. The [ and ] should be replaced by the < and > because the board sees it as HTML commands so you can't see them.

Code:

[occasion]
[fotos]
[foto nr="1">9120_TBGX66_1.JPG[/foto]
[foto nr="2">9120_TBGX66_2.JPG[/foto]
[/fotos]
[/occasion]



I used some XML code from this thread that has code by Shawn, Jose and Jim but I do not see a solution in there for reading both lines from the example file above. Is there a solution for this so I can read both lines and not just the first line?
The nr="x" part is can be different each time. It can be from 1 to 2 but it can also be from 1 to 50. Preferably all lines should be in different variables.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#160022 - 2006-03-30 01:28 PM Re: Reading XML formatted files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Mart - I just ran into this exact same issue myself, the other day - have the perfect solution - standby.
Top
#160023 - 2006-03-30 01:30 PM Re: Reading XML formatted files
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Cool!
Standing by.....
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#160024 - 2006-03-30 01:40 PM Re: Reading XML formatted files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Heres the code - the "secret" is to use something called XQL - XML Query Language - which allows one to get at those "duplication looking" tags with unique ID or NAME attributes.

XQL is to XML, what WQL is to WMI.

XQL is already built into Microsoft.XmlDom but warning - it can be a little fussy getting the exact syntax correct. but once nailed, its good to go.

Here is the code I just tested with your posted XML file:

Code:

Break On

$xml = XmlOpen(".\t.xml")

$filename1 = XmlReadValue($xml, "occasion/fotos/foto[@@nr='1']")

?"Filename1=" $filename1

$filename2 = XmlReadValue($xml, "occasion/fotos/foto[@@nr='2']")

?"Filename2=" $filename2

Exit 0


Function XmlOpen($filename)

$XmlOpen = CreateObject("microsoft.xmldom")

If Not $XmlOpen
Exit @ERROR
Endif

If Not $XmlOpen.Load($filename)
Exit @ERROR
Endif

EndFunction

Function XmlReadAttribute($xml, $path, $attrib)

Dim $node

$node = $xml.SelectSingleNode($path)

If $node

$XmlReadAttribute = $node.GetAttribute($attrib)

Endif

Exit @ERROR

EndFunction

Function XmlReadValue($xml, $path)

Dim $node

$node = $xml.SelectSingleNode($path)

If $node

$XmlReadValue = $node.Text

Endif

Exit @ERROR

EndFunction


Top
#160025 - 2006-03-30 01:46 PM Re: Reading XML formatted files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Of course, the brute-force approach is to loop through the fotos node, and check each element for nr='whatever' ... think the XQL approach is more eloquent and definitely much faster.
Top
#160026 - 2006-03-30 02:30 PM Re: Reading XML formatted files
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Cool Works great.

I'm a newbie in the world of XML so I learned something today.
Thanx shawn.

BTW: In the thread I linked to Jose was very active. Haven’t seen him around for some time now. Any idea what happened?
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#160027 - 2006-03-30 02:43 PM Re: Reading XML formatted files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
btw not sure if you've run into this or not, but XML (especially XQL) is very case-sensitive ... cant say how many times that has burned me. Looking for some magic switch that might turn that off - haven't found yet.

Jose - I see him on MSN sometimes - haven't chatted with him in a while though. Will ping him next time I see him. Know he's recently gone through a career change, that may have something to do with.

Top
#160028 - 2006-03-30 03:01 PM Re: Reading XML formatted files
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Case sensitive, hmmmm no problems with that (yet). Thanx for the warning.

Ah.... Jose had a career change. That might be the reason why he does not visit korg anymore.
I always liked his way of looking at things and his solutions to the questions that come up here at korg. Oh well, maybe we will see him back here one day who knows.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#160029 - 2006-10-05 04:25 PM Re: Reading XML formatted files
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Thanx again Shawn. This bloody case sensitive stuff came back to bite me today and it hurts like h#ll . Looking at some code and an XML file for several hours before I remembered something about upper and lower case. Looked at the source file, changed the script and its up and running.

@$%$##%^%@%$


Edited by Mart (2006-10-05 04:26 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2366 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.045 seconds in which 0.018 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org