An extension for XMLParser's DOM.
Three functionalities:
- base class for DOM visitors by the element's types (Document, Element, CData, String, ...)
- delegating visitor with which you can visit the DOM by element's name
- typed retrieval of node values and attribute values
<people>
<docId>cc254e89-569e-5b46-a6e9-4a06de4d9da0</docId>
<person name="Sarah" age="32" />
<person name="Carl" age="27" />
</people>
The XML above can be visited with a visitor such as the following one:
MyVisitor>>visitPeople: aPeopleElement
"nothing to do"
MyVisitor>>visitDocId: aDocIdElement
aDocIdElement uuidValue "an instace of UUID('cc254e89-569e-5b46-a6e9-4a06de4d9da0')"
MyVisitor>>visitPerson: aPersonElement
aPersonElement stringAt: #name "returns string 'Sarah'"
aPersonElement numberAt: #age "returns number '32'"
Subclass XMLDOMVisitor
to create your own visitor.
Example: Look at in-image class XMLDOMTestVisitor
.
Use XMLDOMElementVisitor
to delegate the visiting by the element's localName
(without namespace) as the selector, e.g.
visitor := MyVisitor new.
XMLDOMElementVisitor new
elementVisitor: visitor;
visit: dom.
Then e.g. for an element <person>
, the visitPerson:
message will be sent to your MyVisitor
.
If you specify XMLDOMTypedElement
as the parsing node, then you can retrieve values with a specific type.
dom := (XMLDOMParser on: aStream)
nodeFactory: (XMLPluggableElementFactory new elementClass: XMLDOMTypedElement);
parseDocument.
Example:
<myElement isRandom="true">12.7</myElement>
will be parsed into XMLDOMTypedElement
instance.
element booleanAt: #isRandom. "-> true"
element stringAt: #isRandom. "-> 'true'"
element numberValue. "-> 12.7"
element stringValue. "-> '12.7'"
Look into the protocols typed attribute accessing
and typed value accessing
of XMLDOMTypedElement
.
The following conversions are supported: boolean
, date
, number
, string
, symbol
, and uuid
.
Extras:
booleanAt:
/booleanValue
will returntrue
for strings'true'
,'1'
, or 'yes'.XMLDOMTypedElement>>isEmptyValue
will return boolean if the element has any content. (e.g.<el></el>
versus<el>data</el>
)
Metacello new
baseline: 'XMLDOMVisitor';
repository: 'github://OpenPonk/xml-dom-visitor/repository';
load.