[REBOL] Re: Rebol & XML
From: AJMartin:orcon at: 6-Aug-2003 21:31
I figured out where I went wrong (I was 'push-ing too early). This function
loads XML very nicely.
Load-XML: function [
[catch] "Loads XML as a Rebol compatible block of values."
XML [string! file!] "The XML string or file."
] [Content Stack Attribute Value Attribute_Value^
Text^ Declaration^ Name Text Element^] [
Content: make block! 10
Stack: make block! 10
Attribute_Value^: [
WS* copy Attribute [
some Alpha opt [#":" some Alpha]
] {="} copy Value to #"^"" skip (
insert tail Content reduce [
to issue! Attribute
Value
]
)
]
Text^: complement charset #"<"
Declaration^: [
"<?xml" (Name: 'xml) any Attribute_Value^ WS? "?>" (
Content: reduce [Name Content]
)
]
Element^: [
"<!--" thru "-->" | #"<" Z: copy Name [
some Alpha (
push/only Stack reduce [to word! Name Content]
Content: make block! 6
)
] any [Attribute_Value^] WS? [
"/>" | #">" (push Stack Name) [
some [
copy Text some Text^ (
if not empty? trim Text [
insert tail Content Text
]
)
| Element^
]
] "</" (Name: pop Stack) Name WS? #">"
] (
insert tail last first Stack reduce [
to word! Name
either 1 = length? Content [
first Content
] [
Content
]
]
set [Name Content] pop Stack
)
]
if file? XML [
XML: read XML
]
all [
parse/all/case XML [
WS? Declaration^
WS? Element^
WS? end
]
Content
]
]
With the example from Rebol HQ:
XML: {
<?xml version="1.0"?>
<PERSON>
<NAME>Fred</NAME>
<AGE>24</AGE>
<ADDRESS>
<STREET>123 Main Street</STREET>
<CITY>Ukiah</CITY>
<STATE>CA</STATE>
</ADDRESS>
</PERSON>
}
probe X: load-XML XML
Here's the result:
[xml [#version "1.0"] PERSON [NAME "Fred" AGE "24" ADDRESS [STREET "123 Main
Street" CITY "Ukiah" STATE "CA"]]]
And with my example XML fragment:
XML: {<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Andrew John Martin</Author>
<LastAuthor>Andrew John Martin</LastAuthor>
<Created>2003-08-05T02:10:56Z</Created>
<LastSaved>2003-08-05T02:10:57Z</LastSaved>
<Company>Colenso High School</Company>
<Version>10.4219</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<DownloadComponents/>
<LocationOfComponents HRef="file:///\\"/>
</OfficeDocumentSettings>
</Workbook>
}
Here's the results of various path! values:
>> x/workbook/documentproperties/author
== "Andrew John Martin"
>> x/workbook/officedocumentsettings/#xmlns
== "urn:schemas-microsoft-com:office:office"
:)
Andrew J Martin
ICQ: 26227169
http://www.rebol.it/Valley/
http://valley.orcon.net.nz/
http://Valley.150m.com/