php - Parsing XML-document (odt-file): How to step through elements to fill an array -
php - Parsing XML-document (odt-file): How to step through elements to fill an array -
i seek parse xml-document (content.xml of odt-file).
$reader = new xmlreader(); if (!$reader->open("content.xml")) die("failed open 'content.xml'"); // step through text:h , text:p elements set them array while ($reader->read()){ if ($reader->nodetype == xmlreader::element && ($reader->name === 'text:h' || $reader->name === 'text:p')) { echo $reader->expand()->textcontent; // set text array in right order... } } $reader->close();
first of need little hint how step correctly through elements of xml-file. in effort can step through text:h-elements, how other elements (text:p), without messing everything...
nevertheless i'll show final target @ all. please don't think i'm asking finish solution. wrote downwards show construction need. want solve problem step step
the content of xml-file like:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> [...] <office:body> <office:text text:use-soft-page-breaks="true"> <text:h text:style-name="p1" text:outline-level="2">chapter 1</text:h> <text:p text:style-name="standard">lorem ipsum. </text:p> <text:h text:style-name="heading3" text:outline-level="3">subtitle 1</text:h> <text:p text:style-name="standard"><text:span text:style-name="t2">something 1:</text:span> lorem.</text:p> <text:p text:style-name="standard"><text:span text:style-name="t3">something 2:</text:span><text:s/>lorem ipsum.</text:p> <text:p text:style-name="standard"><text:span text:style-name="t4">something 3:</text:span> lorem ipsum.</text:p> <text:h text:style-name="heading3" text:outline-level="3">subtitle 2</text:h> <text:p text:style-name="standard"><text:span text:style-name="t5">10</text:span><text:span text:style-name="t6">:</text:span><text:s/>text (100%)</text:p> <text:p text:style-name="explanation">further informations.</text:p> <text:p text:style-name="standard">9.7:<text:s/>text (97%)</text:p> <text:p text:style-name="explanation">further informations.</text:p> <text:p text:style-name="standard"><text:span text:style-name="t9">9.1:</text:span><text:s/>text (91%)</text:p> <text:p text:style-name="explanation">further informations.</text:p> <text:p text:style-name="explanation">more furter informations.</text:p> [subtitle 3 , 4] <text:h text:style-name="heading3" text:outline-level="3">subtitle 5</text:h> <text:p text:style-name="standard"><text:span text:style-name="t5">10</text:span><text:span text:style-name="t6">:</text:span><text:s/>text (100%)</text:p> <text:p text:style-name="explanation">further informations.</text:p> <text:p text:style-name="standard">9.7:<text:s/>text (97%)</text:p> <text:p text:style-name="explanation">further informations.</text:p> <text:p text:style-name="standard"><text:span text:style-name="t9">9.1:</text:span><text:s/>text (91%)</text:p> <text:p text:style-name="explanation">further informations.</text:p> <text:p text:style-name="explanation">more furter informations.</text:p> <text:h text:style-name="heading3" text:outline-level="3">references</text:h> <text:list text:style-name="lfo44" text:continue-numbering="true"> <text:list-item><text:p text:style-name="p25">blabla et al., title p. 580-586</text:p></text:list-item> <text:list-item><text:p text:style-name="p25">blabla et al., title p. 580-586</text:p></text:list-item> <text:list-item><text:p text:style-name="p25">blabla et al., title p. 580-586</text:p></text:list-item> <text:list-item><text:p text:style-name="p25">blabla et al., title p. 580-586</text:p></text:list-item> </text:list> [multiple chapter this] </office:text> </office:body>
you see, "subchapters" have standard-elements , optional explanation-element (also multiple explanation-elements 1 standard possible). construction same...
my final target split informations array-output this:
array() { [1]=> array() { ["chapter"]=> string() "chapter 1" ["content"]=> array() { [0]=> array() { ["subchapter"]=> string() "description" ["content"]=> array() { [0]=> array() { ["standard"]=> string() "lorem ipsum." ["explanation"]=> string(0) "" } } } [1]=> array() { ["subchapter"]=> string() "subtitle 1" ["content"]=> array() { [0]=> array() { ["standard"]=> string() "something 1: lorem." ["explanation"]=> string() "" } [1]=> array() { ["standard"]=> string() "something 2: lorem ipsum." ["explanation"]=> string() "" } [2]=> array() { ["standard"]=> string() "something 2: lorem ipsum." ["explanation"]=> string() "" } } } [2]=> array() { ["subchapter"]=> string() "subtitle 2" ["content"]=> array() { [0]=> array() { ["standard"]=> string() "10: text (100%)" ["explanation"]=> string() "further informations." } [and on]
edit:
i can see issue now, editing question:
in while loop
while ($reader->read()){ }
you have couple of functions available nodes , values:
$reader->value
will give value (eg 'subtitle 1')
$reader->getattribute('text:style-name')
should 'heading3' part
putting altogether, want within while loop [pseudocode]:
// set index $i = 0; // parts fromt xml need $name = $reader->name; $attrib = $reader->getattribute('text:style-name'); $value = $reader->value; // if attribute 'p1', increment our index, need new indentation in our array if($value == 'p1'){ $i++; } $array[$i][$attrib]=$reader->value;
note indentation 1 level - looks need 4 levels, should have 4 indexes [$i,$k,$k,$l] , check each 1 against each thing needs indented - p1,heading3, etc
you might end
$array[$i][$j][$k] = $reader->value;
or like. remember re-set sub-indexes when incrment higher index (eg if $i++, set $j=0, $k=0, etc)
previous answers below:
simplexml (probably) in few lines [if construction of xml file nested right way, which, after quick look, appears be]: http://php.net/manual/en/book.simplexml.php
$xml = simplexml_load_file('content.xml'); $json = json_encode($xml); $array = json_decode($json,true); print_r($array);
edit: can utilize xpath simplexml, , can things like
echo $xml->{office:body}->{office:text}->{text.h}
php xml xml-parsing xmlreader
Comments
Post a Comment