[REBOL] Re: XML-Parsing?!?
From: joel:neely:fedex at: 25-Oct-2000 12:36
Hi, Patrick,
Errrmmm... your sample isn't legal XML!!
[rebol-bounce--rebol--com] wrote:
[snip]
> All I need is the possibility to simply read an external XML-File and
> extract values from certain tags like this:
>
> Pseudo: print the value of Prozesse/cre
>
> ------------------------BEGIN XML----------------------------------
> <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
> <!-- Diese Datei enthält die Stati der xxx relevanten Prozesse -->
> <!-- 0=OK, 1=Fehler/fehlend, 2=Unbekannt -->
>
> <test1 name = "Patrick" />
> <test2 name = "Scheller" />
> <Prozesse>
> <xxx = "1" />
> <cre = "2" />
> <ora = "3" />
> <xxxrec = "4" />
> <db_ppb = "5" />
> </Prozesse>
>
> <!-- Ende der Datei -->
> -------------------------END XML-----------------------------------
>
An XML document must have exactly one top-level XML element. You
have three (<test1 ...>, <test2 ...>, and <Prozesse ...>). In addition,
you have what appear (by intent) to be five content elements under
<Prozesse ...> which are not valid. If elements, they would have to
be written something like:
<xxx value="1"/>
<cre value="2"/>
...etc...
If they were intended to be attributes of the <Prozesse ...> element,
then you need to lose the "<" and "/>" bracketing around them, and
embed them INSIDE the <Prozesse ...> tag itself.
For the sake of furthering the discussion, I will guess what you meant as:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!-- Diese Datei enthält die Stati der xxx relevanten Prozesse -->
<!-- 0=OK, 1=Fehler/fehlend, 2=Unbekannt -->
<datei>
<test1 name = "Patrick" />
<test2 name = "Scheller" />
<Prozesse
xxx = "1"
cre = "2"
ora = "3"
xxxrec = "4"
db_ppb = "5" />
</datei>
<!-- Ende der Datei -->
(that is, assuming that you wanted attributes). With that assumption,
we can use something like the following:
REBOL []
prozesse: make object! [
select-element-attribute: function [
"collect all values for given element/attribute names"
b [block!] "xml document"
e [string!] "element name"
a [string!] "attribute name"
][
buffer
attval
][
buffer: copy []
if all [
found? second b
found? attval: select second b a
][
append buffer attval
]
if found? third b [
foreach sub third b [
if block? sub [
append buffer select-element-attribute sub e a
]
]
]
buffer
]
]
to do this:
>> foo: parse-xml read %sample3.xml
XML Version: 1.0
== [document none [["datei" none ["^/" ["test1" ["name" "Patrick"] ...
>> prozesse/select-element-attribute foo "Prozesse" "cre"
== ["2"]
Hope this helps!
-jn-
--
; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]