PHP/XML/XML Parser
Jump to navigation
Jump to search
Official: XML Parser
This library consists of an opaque (no public methods or members) class that is read/written by a collection of PHP functions.
The result of parsing XML content consists of two arrays:
- a flat array of all the tags in the content, with their attributes and values
- an "index" of all the locations in the array where each type of tag occurs
I haven't been able to puzzle out whether these arrays preserve the structure of the document.
Example PHP Code
$onXML = xml_parser_create();
$ok = (bool)xml_parse_into_struct($onXML, $s, $arVals, $arIdx);
if (!$ok) {
$nErr = xml_get_error_code($onXML);
$sErr = xml_error_string($nErr);
$nLine = xml_get_current_line_number($onXML);
$sErr = "could not parse string data: XML error $nErr (line $nLine): ".$sErr;
}
If successful, the main content is in $arVals and the index is in $arIdx. If not successful, $sErr will show an error message indicating the location in the XML source of the problem.
There must be only one base-level tag (e.g. <data>
below), or there will be an error (something like "premature ending"). Comments can be outside of this, however.
Example XML Data
<!--
WHAT: experimental UI-definition data
HISTORY:
* 2024-06-19 started
* 2024-07-07 adapting (not working yet) for dbak
-->
<data format="wooz-ui-def" name="ui-def">
<meta>
<docs>
<link url="https://wooz.dev/Futilities/" /> <!-- no documentation yet -->
</docs>
<summary>backs up or restores one or more databases</summary>
</meta>
<request>
<option type="flag" name="do.read">
<summary>back up the database(s)</summary>
<cue>-dr</cue>
<cue>--do-read</cue>
</option>
<option type="flag" name="do.write">
<summary>write the given backups to the given database connections</summary>
<cue>-dw</cue>
<cue>--do-write</cue>
</option>
<option type="flag" name="get.list">
<summary>just list the available databases</summary>
<cue>-gl</cue>
<cue>--get-list</cue>
</option>
</request>
</data>
This results in the following arrays:
Values ($arVals) | Index ($arIdx) |
---|---|
[0] => Array ( [tag] => DATA [type] => open [level] => 1 [attributes] => Array ( [FORMAT] => wooz-ui-def [NAME] => ui-def ) [value] => ) [1] => Array ( [tag] => META [type] => open [level] => 2 [value] => ) [2] => Array ( [tag] => DOCS [type] => open [level] => 3 [value] => ) [3] => Array ( [tag] => LINK [type] => complete [level] => 4 [attributes] => Array ( [URL] => https://wooz.dev/Futilities/ ) ) [4] => Array ( [tag] => DOCS [value] => [type] => cdata [level] => 3 ) [5] => Array ( [tag] => DOCS [type] => close [level] => 3 ) [6] => Array ( [tag] => META [value] => [type] => cdata [level] => 2 ) [7] => Array ( [tag] => SUMMARY [type] => complete [level] => 3 [value] => backs up or restores one or more databases ) [8] => Array ( [tag] => META [value] => [type] => cdata [level] => 2 ) [9] => Array ( [tag] => META [type] => close [level] => 2 ) [10] => Array ( [tag] => DATA [value] => [type] => cdata [level] => 1 ) [11] => Array ( [tag] => REQUEST [type] => open [level] => 2 [value] => ) [12] => Array ( [tag] => OPTION [type] => open [level] => 3 [attributes] => Array ( [TYPE] => flag [NAME] => do.read ) [value] => ) [13] => Array ( [tag] => SUMMARY [type] => complete [level] => 4 [value] => back up the database(s) ) [14] => Array ( [tag] => OPTION [value] => [type] => cdata [level] => 3 ) [15] => Array ( [tag] => CUE [type] => complete [level] => 4 [value] => -dr ) [16] => Array ( [tag] => OPTION [value] => [type] => cdata [level] => 3 ) [17] => Array ( [tag] => CUE [type] => complete [level] => 4 [value] => --do-read ) [18] => Array ( [tag] => OPTION [value] => [type] => cdata [level] => 3 ) [19] => Array ( [tag] => OPTION [type] => close [level] => 3 ) [20] => Array ( [tag] => REQUEST [value] => [type] => cdata [level] => 2 ) [21] => Array ( [tag] => OPTION [type] => open [level] => 3 [attributes] => Array ( [TYPE] => flag [NAME] => do.write ) [value] => ) [22] => Array ( [tag] => SUMMARY [type] => complete [level] => 4 [value] => write the given backups to the given database connections ) [23] => Array ( [tag] => OPTION [value] => [type] => cdata [level] => 3 ) [24] => Array ( [tag] => CUE [type] => complete [level] => 4 [value] => -dw ) [25] => Array ( [tag] => OPTION [value] => [type] => cdata [level] => 3 ) [26] => Array ( [tag] => CUE [type] => complete [level] => 4 [value] => --do-write ) [27] => Array ( [tag] => OPTION [value] => [type] => cdata [level] => 3 ) [28] => Array ( [tag] => OPTION [type] => close [level] => 3 ) [29] => Array ( [tag] => REQUEST [value] => [type] => cdata [level] => 2 ) [30] => Array ( [tag] => OPTION [type] => open [level] => 3 [attributes] => Array ( [TYPE] => flag [NAME] => get.list ) [value] => ) [31] => Array ( [tag] => SUMMARY [type] => complete [level] => 4 [value] => just list the available databases ) [32] => Array ( [tag] => OPTION [value] => [type] => cdata [level] => 3 ) [33] => Array ( [tag] => CUE [type] => complete [level] => 4 [value] => -gl ) [34] => Array ( [tag] => OPTION [value] => [type] => cdata [level] => 3 ) [35] => Array ( [tag] => CUE [type] => complete [level] => 4 [value] => --get-list ) [36] => Array ( [tag] => OPTION [value] => [type] => cdata [level] => 3 ) [37] => Array ( [tag] => OPTION [type] => close [level] => 3 ) [38] => Array ( [tag] => REQUEST [value] => [type] => cdata [level] => 2 ) [39] => Array ( [tag] => REQUEST [type] => close [level] => 2 ) [40] => Array ( [tag] => DATA [value] => [type] => cdata [level] => 1 ) [41] => Array ( [tag] => DATA [type] => close [level] => 1 ) |
[DATA] => Array ( [0] => 0 [1] => 10 [2] => 40 [3] => 41 ) [META] => Array ( [0] => 1 [1] => 6 [2] => 8 [3] => 9 ) [DOCS] => Array ( [0] => 2 [1] => 4 [2] => 5 ) [LINK] => Array ( [0] => 3 ) [SUMMARY] => Array ( [0] => 7 [1] => 13 [2] => 22 [3] => 31 ) [REQUEST] => Array ( [0] => 11 [1] => 20 [2] => 29 [3] => 38 [4] => 39 ) [OPTION] => Array ( [0] => 12 [1] => 14 [2] => 16 [3] => 18 [4] => 19 [5] => 21 [6] => 23 [7] => 25 [8] => 27 [9] => 28 [10] => 30 [11] => 32 [12] => 34 [13] => 36 [14] => 37 ) [CUE] => Array ( [0] => 15 [1] => 17 [2] => 24 [3] => 26 [4] => 33 [5] => 35 ) |