Prev | Current Page 399 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"


Traversing a document
Traversing an XML document is easy if you know the structure of the XML you are using. For example,
suppose that you have to retrieve the URL of the link in the XML shown in the ??? XML Anatomy ??? section.
First, the document content must be accessible in the code; usually a good idea is to read it from some
external source like a local file or a remote resource but this is quite often platform specific. To have a
unified way to access the document, it is embedded in the code using a haXe resource. The XML file is
located in an assets folder and embedded at compile time adding the following switch to the compiler
command:
-resource assets/sample.xml@sample
The code to access the link attribute uses many of the fields described previously.
class Main
{
public static function main()
{
var xml = Std.resource(???sample???);
var doc = Xml.parse(xml);
var html = doc.firstElement();
var htmlchildren = html.elements();
htmlchildren.next(); // skip head element
var body = htmlchildren.


Pages:
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411