[ Team LiB ] Previous Section Next Section

Workshop

Quiz

1:

How would you acquire a parser resource?

2:

Which arguments will the XML parser pass to an element start handler?

3:

How would you turn off the feature that converts all element names to uppercase characters?

4:

How would you get a current line number while an XML document is being parsed?

5:

How would you get a DomDocument object using an existing XML file?

6:

Given an DomElement object, how would you add a child element to your tree?

7:

Which object would you use to apply XSL to an XML document?


Answers

A1:

You can get a parser resource with the xml_parser_create() function, like so:

$parser = xml_parser_create();

A2:

The user-defined element start handler function automatically is passed a parser resource, the name of the element which is starting, and an array of attributes.

A3:

You can use the xml_parser_set_option() function to disable case folding, like so:

xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );

A4:

The xml_get_current_line_number() function returns the current line number.

A5:

You can instantiate a DomDocument object directly using the new keyword. You can then use the loadXML() method to import XML from a file. Here's how:

$xml_doc = new DomDocument ("1.0" );
$xml_doc->loadXML( file_get_contents("./listing22.1.xml") );

A6:

You can use the appendChild() method to add an element to a tree of objects, as shown here:

$doc = new DomDocument();
$rootel = $doc->createElement("banana-news");
$doc->appendChild( $rootel );

A7:

The XsltProcessor class returns an object that can be used to process XSLT.


    [ Team LiB ] Previous Section Next Section