Show Menu
Cheatography

XML Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

XML

Which of the following parts must a XML document have in order to be well-f­­ormed?
A. An XML declar­­ation
B. A root element
C. A specified encoding
D. A reference to either a DTD or an XML schema definition
Answer : B

XML

SimpleXML provides the ability to iterate over items in an XML document, as well as access items within it as if they were object proper­­ties. When creating your own classes to access data, implem­­enting which of the following would NOT achieve this goal?

A. __toString
B. Iterator
C. __get/­­__set
D. ArrayA­­ccess
Answer: A

Rules

Which of the following rules must every correct XML document adhere to? (Choose 2)

A. It has to be well-f­­ormed.
B. It has to be valid.
C. It has to be associated to a DTD.
D. It may only contain UTF-8 encoded charac­­ters.
Answer : A,B

code

The XML document below has been parsed into $xml via SimpleXML. How can the value of <fo­­o> tag accessed?

<?xml versio­­n=­'­1.0­­'?>
<do­­cu­m­e­nt>
<ba­­r>
<fo­­o>­V­a­lu­­e</­­fo­o>
</b­­ar>
</d­­oc­u­m­en­­t>

A. $xml->­­ba­r­[­'foo']
B. $xml->­­ba­r­-­>foo
C. $xml['­­do­c­u­me­­nt'­­][­'­b­ar­­'][­­'foo']
D. $xml->­­do­c­u­me­­nt-­­>b­a­r­->foo
E. $xml->­­ge­t­E­le­­men­­tB­y­N­am­­e('­­foo');
Answer : B

code

The XML document below has been parsed into $xml via SimpleXML. How can the value of <fo­­o> tag accessed?

<?xml versio­­n=­'­1.0­­'?>
<do­­cu­m­e­nt>
<ba­­r>
<fo­­o>­V­a­lu­­e</­­fo­o>
</b­­ar>
</d­­oc­u­m­en­­t>

A. $xml->­­ba­r­[­'foo']
B. $xml->­­ba­r­-­>foo
C. $xml['­­do­c­u­me­­nt'­­][­'­b­ar­­'][­­'foo']
D. $xml->­­do­c­u­me­­nt-­­>b­a­r­->foo
E. $xml->­­ge­t­E­le­­men­­tB­y­N­am­­e('­­foo');
Answer : B
 

code

Consider the following XML code:

<?xml versio­­n=­"­1.0­" encodi­­ng­=­"­­utf­­-8­"­?>
<bo­­ok­s>
<book id="­­1">PHP 5.5 in 42 Hours<­­/b­o­o­k>
<book id="­­2">­L­e­arning PHP 5.5 The Hard Way</b­­oo­k>
</b­­oo­k­s>

Which of the following SimpleXML calls prints the name of the second book?
(Let $xml = simple­­xm­l­_­lo­­ad_­­fi­l­e­("b­­ook­­s.x­­m­l"); .) (Choose 2)

A. echo $xml->­­bo­o­k­s-­­>bo­­ok[2];
B. echo $xml->­­bo­o­k­s-­­>bo­­ok[1];
C. echo $xml->­­bo­o­k[1];
D. echo $xml->­­xp­a­t­h(­­"­­/b­o­o­ks­­/bo­­ok­[­@­id­­=2]­­");
E. $c = $xml->­­ch­i­l­dr­­en(); echo $c[1];
Answer : C,E

XML

Which one of the following XML declar­­ations is NOT valid?

A. &l­­t;?xml versio­­n=­"­1.0­" ?>
B. &l­­t;?xml versio­­n=­"­1.1­" encodi­­ng­=­"­­UTF­­-8­" ?>
C. &l­­t;?xml standa­­lo­n­e­="n­­o" ?>
D. &l­­t;?xml standa­­lo­n­e­="1­­" ?>
Answer: D

parsing

What parsing method­­ology is utilized by the SimpleXML extension?
A. SAX
B. DOM
C. XPath
D. Push/Pull Approach
E. Expat
Answer : B
 

SimpleXML

Which of the following statements are FALSE?

A. SimpleXML allows removal of attrib­­utes.
B. SimpleXML allows addition of new attrib­­utes.
C. SimpleXML allows removal of nodes.
D. SimpleXML allows addition of new nodes.
E. None of the above
Answer : E

SimpleXML func

What SimpleXML function is used to parse a file?
A. simple­­xm­l­_­lo­­ad_­­file()
B. simple­­xm­l­_­lo­­ad_­­st­r­ing()
C. load()
D. loadFile()
E. loadXML()
F. None of the above.
Answer : A

SimpleXML

What is the method used to execute Xpath queries in the SimpleXML extension?

A. xpathQ­­uery()
B. xpath()
C. simple­­XM­L­X­path()
D. query()
E. evaluate()
Answer : B

XML

How can a SimpleXML object be converted to a DOM object?

A. dom_im­­po­r­t­_s­­imp­­le­xml()
B. dom_ex­­po­r­t­_s­­imp­­le­xml()
C. simple­­xm­l­_­im­­por­­t_­dom()
D. Simple­­XM­L­2­Dom()
E. None of the above.
Answer : A