Help with reading/parsing an XML file in GST 3.2

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with reading/parsing an XML file in GST 3.2

Rick Flower-2
So.. I was pestering Paolo via email about help with reading small XML
files and parsing them.. Below are his suggestions -- mind you the
files to
be parsed are WELL under 1Kb if it matters.

>You can create a FilePath object (you will actually get a File) using
> #asFile.
>
>So, something like this should work:
>
>content := 'foo.xml' asFile contents.
>parser := XML.XMLParser new.
>parser validate: false.
>parser processDocumentString: content
>
>I don't have the source at hand, but it's better if you avoid reading
>the whole contents and instead use a Stream. It should be something
>like this:
>
>stream := 'foo.xml' asFile readStream.
>parser := XML.XMLParser new.
>parser validate: false.
>parser processDocumentStream: stream

I've been able to verify that I can do this without issue:

  stream := './path-to/foo.xml' asFile readStream.
  Transcript show: stream.

but when I add the last few lines and have this code snippet it fails :

  stream := './path-to/foo.xml' asFile readStream.

  parser := XML.XMLParser new.
  parser validate: false.
  parser processDocumentStream: stream.

Recompiling classes...
Recompiling class: XML.Attribute
Recompiling selector: #key
Recompiling selector: #value
Recompiling selector: #value:
Recompiling selector: #printOn:
Recompiling selector: #printCanonicalOn:
Recompiling selector: #simpleDescription
Recompiling selector: #isAttribute
Recompiling selector: #isLike:
Recompiling selector: #tag:
Recompiling selector: #tag
Recompiling selector: #expandedName
Recompiling selector: #characterData
Recompiling selector: #name:value:
Recompiling class: XML.Attribute class
Recompiling selector: #name:value:
Object: XMLParser new "" error: did not understand
#processDocumentStream:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
XML.XMLParser(Object)>>doesNotUnderstand: #processDocumentStream:
(SysExcept.st:1436)
UndefinedObject>>executeStatements
(../../../eps_fltinfra/sdfhelper.st:37)

So, I did a quick search through the XML package (~/packages/xml/...)
for
processDocumentStream but found nothing..

Am I missing something? If someone could point me in the right
direction,
that would be great..

--Rick

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Help with reading/parsing an XML file in GST 3.2

Rick Flower-2
 

Nevermind.. I found that if I changed #processDocumentStream: to
#parse: that it works fine.. I searched the archives the other day but
didn't notice one post from someone else using that method..

So, for
completeness for anyone that might come after me here's the scoop :


With this XML file (for example) :

]>

foo
bar
baz

You can then use
this code to read & parse it :

[PackageLoader fileInPackage: #XML] on:
Error do: [:ex | ex return].

...

| stream |

 stream :=
'./identity.xml' asFile readStream.
 parser := XML.XMLParser new.

parser validate: false.
 parser parse: stream.

 
_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Help with reading/parsing an XML file in GST 3.2

Rick Flower-2
Trying one more time -- since part of it got eaten.. I think I forgot
to switch to plain text.. Doh!
The primer I used to build the internal DTD can be found here:
http://www.w3schools.com/dtd/dtd_intro.asp

-------------------------------------------------------------------------------------------------------
Nevermind.. I found that if I changed #processDocumentStream: to
#parse: that it works fine.. I searched the archives the other day but
didn't notice one post from someone else using that method..

So, for completeness for anyone that might come after me here's the
scoop :

With this XML file (for example) :

<?xml version="1.0"?>
<!DOCTYPE identity [
<!ELEMENT identity (name1, name2, name3)>
<!ELEMENT name1(#PCDATA)>
<!ELEMENT name2(#PCDATA)>
<!ELEMENT name3(#PCDATA)>
]>
<identity>
<name1>foo</name1>
<name2>bar</name2>
<name3>baz</name3>
</identity>



You can then use this code to read & parse it :

[PackageLoader fileInPackage: #XML]  on: Error do: [:ex | ex return].

...

| stream |

     stream := './identity.xml' asFile readStream.
     parser := XML.XMLParser new.
     parser validate: false.
     parser parse: stream.



_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Help with reading/parsing an XML file in GST 3.2

Rick Flower-2
On 05.09.2012 08:16, Rick Flower wrote:

...
> stream := './identity.xml' asFile readStream.
> parser := XML.XMLParser new.
> parser validate: false.
> parser parse: stream.

For those like me that stumbled around a bit more on the XML parsing,
once you've
got a parser object that is good to go, you can access the docroot and
associated
items (as mentioned on the help page entitled "Building a DOM from
XML") using the
following snippet :

doc := parser document

Once you have that you can get the items of interest as mentioned
earlier :

item1 := (doc root elementNamed: 'foo') characterData.

Seems to work OK! I now believe I'm good to go.. Hopefully this helped
someone else
besides me.. :-)

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Help with reading/parsing an XML file in GST 3.2

Rick Flower-2
 

My bad.. Change above reference of "foo" with "name1".. Sorry..
_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Help with reading/parsing an XML file in GST 3.2

Rick Flower-2
 

Paolo .. Would you like me to update the docs to include the
missing pieces for those people like me that are more on the beginners
side of the world? Just thought I'd ask..
_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk