From 48bedcaea089e4d7f8b94fc89d502755615544c6 Mon Sep 17 00:00:00 2001 From: Ken Date: Fri, 23 Jan 2004 16:17:17 +0000 Subject: [PATCH] Added new SimpleXML reference. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@149543 c90b9560-bf6c-de11-be94-00142212c4b1 --- .../functions/simplexml-element-asXML.xml | 93 +++++++ .../simplexml-element-attributes.xml | 71 ++++++ .../functions/simplexml-element-children.xml | 42 ++++ .../functions/simplexml-element-xpath.xml | 95 ++++++++ .../functions/simplexml-load-dom.xml | 50 ++++ .../functions/simplexml-load-file.xml | 84 +++++++ .../functions/simplexml-load-string.xml | 92 +++++++ reference/simplexml/reference.xml | 226 ++++++++++++++++++ 8 files changed, 753 insertions(+) create mode 100644 reference/simplexml/functions/simplexml-element-asXML.xml create mode 100644 reference/simplexml/functions/simplexml-element-attributes.xml create mode 100644 reference/simplexml/functions/simplexml-element-children.xml create mode 100644 reference/simplexml/functions/simplexml-element-xpath.xml create mode 100644 reference/simplexml/functions/simplexml-load-dom.xml create mode 100644 reference/simplexml/functions/simplexml-load-file.xml create mode 100644 reference/simplexml/functions/simplexml-load-string.xml create mode 100644 reference/simplexml/reference.xml diff --git a/reference/simplexml/functions/simplexml-element-asXML.xml b/reference/simplexml/functions/simplexml-element-asXML.xml new file mode 100644 index 0000000000..666e6e7789 --- /dev/null +++ b/reference/simplexml/functions/simplexml-element-asXML.xml @@ -0,0 +1,93 @@ + + + + + simplexml_element->asXML + + Return a well-formed XML string based on SimpleXML element. + + + + Description + + stringsimplexml_element->asXML + + + + The asXML method formats the parent object's data + in XML version 1.0. + + + + Get XML + + + + text + stuff + + + code + + +XML; + +$xml = simplexml_load_string($string); + +echo $xml->asXML(); // textstuff ... + +?> +]]> + + + + + asXML also works on XPATH results: + + + Using asXML() on + <link linkend="function.simplexml-element-xpath">XPATH</link> + results + + + */ +$result = $xml->xpath('/a/b/c'); + +while(list( , $node) = each($result)) { + echo $node->asXML(); // text and stuff +} +?> +]]> + + + + + + + diff --git a/reference/simplexml/functions/simplexml-element-attributes.xml b/reference/simplexml/functions/simplexml-element-attributes.xml new file mode 100644 index 0000000000..4186252e55 --- /dev/null +++ b/reference/simplexml/functions/simplexml-element-attributes.xml @@ -0,0 +1,71 @@ + + + + + simplexml_element->attributes + + Identifies an element's attributes. + + + + Description + + object simplexml_elementsimplexml_element->attributes + stringdata + + + This function provides the attributes and values defined within an xml tag. + + + + Interpret an XML string + + + 1 + +XML; + +$xml = simplexml_load_string($string); +foreach($xml->foo[0]->attributes() as $a => $b) { + echo $a,'="',$b,"\"\n"; +} +?> +]]> + + + This script will display: + + + + + + + + + + diff --git a/reference/simplexml/functions/simplexml-element-children.xml b/reference/simplexml/functions/simplexml-element-children.xml new file mode 100644 index 0000000000..4860529e93 --- /dev/null +++ b/reference/simplexml/functions/simplexml-element-children.xml @@ -0,0 +1,42 @@ + + + + + simplexml_element->children + + Finds children of given node. + + + + Description + + object simplexml_elementsimplexml_element->children + + + + This function finds the children of the element of which it is a member. The result + follows normal iteration rules. + + + + + diff --git a/reference/simplexml/functions/simplexml-element-xpath.xml b/reference/simplexml/functions/simplexml-element-xpath.xml new file mode 100644 index 0000000000..15e6453984 --- /dev/null +++ b/reference/simplexml/functions/simplexml-element-xpath.xml @@ -0,0 +1,95 @@ + + + + + simplexml_element->xpath + + Runs XPATH query on XML data. + + + + Description + + arraysimplexml_element->xpath + stringpath + + + The xpath method searches the SimpleXML node for + children matching the XPATH path. + It always returns an array of simplexml_element objects. + + + + XPATH + + + + text + stuff + + + code + + +XML; + +$xml = simplexml_load_string($string); + +/* Search for */ +$result = $xml->xpath('/a/b/c'); + +while(list( , $node) = each($result)) { + echo '/a/b/c: ',$node,"\n"; +} + +/* Relative paths also work... */ +$result = $xml->xpath('b/c'); + +while(list( , $node) = each($result)) { + echo 'b/c: ',$node,"\n"; +} +?> +]]> + + + This script will display: + + + + + + Notice that the two results are equal. + + + + + + + diff --git a/reference/simplexml/functions/simplexml-load-dom.xml b/reference/simplexml/functions/simplexml-load-dom.xml new file mode 100644 index 0000000000..6bf0a878c3 --- /dev/null +++ b/reference/simplexml/functions/simplexml-load-dom.xml @@ -0,0 +1,50 @@ + + + + + simplexml_load_dom + + + Get a simplexml_element object from a + DOM node. + + + + Description + + object simplexml_elementsimplexml_load_dom + domNodenode + + + + This function takes a node of a DOM + document and makes it into a SimpleXML node. This new object can + then be used as a native SimpleXML element. If any errors occur, + it returns &false;. + + + &warn.undocumented.func; + + + + diff --git a/reference/simplexml/functions/simplexml-load-file.xml b/reference/simplexml/functions/simplexml-load-file.xml new file mode 100644 index 0000000000..86cd82f6a4 --- /dev/null +++ b/reference/simplexml/functions/simplexml-load-file.xml @@ -0,0 +1,84 @@ + + + + + simplexml_load_file + + Interprets an XML file into an object. + + + + Description + + object simplexml_elementsimplexml_load_string + stringfilename + + + This function will convert the well-formed XML document in the file + specified by filename to an object + of class simplexml_element. If any errors occur + during file access or interpretation, the function returns &false;. + + + + Interpret an XML document + + +]]> + + + This script will display, on success: + + + Example Title + ... +) +]]> + + + At this point, you can go about using $xml->title + and any other elements. + + + + + See also: simplexml_load_string + + + + + diff --git a/reference/simplexml/functions/simplexml-load-string.xml b/reference/simplexml/functions/simplexml-load-string.xml new file mode 100644 index 0000000000..0ad7563b5c --- /dev/null +++ b/reference/simplexml/functions/simplexml-load-string.xml @@ -0,0 +1,92 @@ + + + + + simplexml_load_string + + Interprets a string of XML into an object. + + + + Description + + object simplexml_elementsimplexml_load_string + stringdata + + + This function will take the well-formed xml string + data and return an object with properties + containing the data held within the xml document. If any errors + occur, it returns &false;. + + + + Interpret an XML string + + + + Forty What? + Joe + Jane + + I know that's the answer -- but what's the question? + + +XML; + +$xml = simplexml_load_string($string) + +var_dump($xml); +?> +]]> + + + This script will display: + + + Forty What? + [from] => Joe + [to] => Jane + [body] => + I know that's the answer -- but what's the question? +) +]]> + + + At this point, you can go about using $xml->body + and such. + + + + + See also: simplexml_load_file. + + + + + diff --git a/reference/simplexml/reference.xml b/reference/simplexml/reference.xml new file mode 100644 index 0000000000..7506abf1b5 --- /dev/null +++ b/reference/simplexml/reference.xml @@ -0,0 +1,226 @@ + + + + SimpleXML functions + SimpleXML + + +
+ &reftitle.intro; + &warn.experimental; + + The SimpleXML extension provides a very simple and easily usable + toolset to convert XML to an object that can be processed with + normal property selectors and array iterators. + +
+ +
+ &reftitle.install; + + This extension is only available if PHP was configured with + . The + PHP configuration script does this by default. + +
+ +
+ &reftitle.examples; + + Many examples in this reference require an XML string. Instead of + repeating this string in every example, we put it into a file which + we include in each example. This included file is shown in the + following example section. Alternatively, you could create an XML + document and read it with simplexml_load_file. + + + + Include file example.php with XML string + + + + + PHP: Behind the Parser + + + Ms. Coder + Onlivia Actora + + + Mr. Coder + El Actór + + + + So, this language. It's like, a programming language. Or is it a + scripting language? All is revealed in this thrilling horror spoof + of a documentary. + + 7 + 5 + + +XML; +?> +]]> + + + + + The simplicity of SimpleXML appears most clearly when one extracts + a string or number from a basic XML document. + + Getting <literal><plot></literal> + movie[0]->plot; // "So this language. It's like..." +?> +]]> + + + + + + Accessing non-unique elements in SimpleXML + + When multiple instances of an element exist as children of + a single parent element, normal iteration techniques apply. + + node, we echo a separate . + * [foreach() could be used, but for() stands in place + * as using foreach() on large XML documents will + * create undesirable memory overhead.] */ + +$mid_count = count($movies); +for($mid = 0; $mid < $mid_count; ++$mid) { + echo $movies[$mid]->plot,'
'; +} + +?> +]]> +
+
+
+ + + Using attributes + + So far, we have only covered the work of reading element names + and their values. SimpleXML can also access element attributes. + Access attributes of an element just as you would elements of + an array. + + + nodes of the first movie. + * Output the rating scale, too. */ +foreach($xml->movie[0]->rating as $rating) { + switch($rating['type']) { // Get attributes as element indices + case 'thumbs': + echo $rating, ' thumbs up'; + break; + case 'stars': + echo $rating, ' stars'; + break; + } +} +?> +]]> + + + + + + Using XPATH + + SimpleXML includes builtin XPATH support. + To find all <character> elements: + + +xpath('//character') as $character) { + echo $character->name, 'played by ', $character->actor, '
'; +} +?> +]]> +
+ + '//' serves as a wildcard. To specify absolute + paths, omit one of the slashes. + +
+
+ + + Setting values + + Data in SimpleXML doesn't have to be constant. The object allows + for manipulation of all of its elements. + + +movie[0]->actor[0]->age = '21'; + +echo $xml->asXML(); +?> +]]> + + + The above code will output a new XML document, just like the original, + except that the new XML will define Ms. Coder's age as 21. + + + +
+
+ +&reference.simplexml.functions; + +
+ +