I wanted to test xml_parse_into_ struct() so I took the example off of
www.php.net and put this code up on a site:
<?php
$simple = <<<END
<item>
<cbId>345</cbId>
<cbHeadline>H ey there</cbHeadline>
<cbMainContent> God is not dead, he is merely
sleeping.</cbMainContent>
</item>
<item>
<cbId>432</cbId>
<cbHeadline>Wha t a dog, what a dog</cbHeadline>
<cbMainContent> In the manger.</cbMainContent>
</item>
END;
$p = xml_parser_crea te();
xml_parse_into_ struct($p, $simple, $vals, $index);
xml_parser_free ($p);
echo "Index array\n";
print_r($index) ;
echo "\nVals array\n";
print_r($vals);
?>
However, what I got back made no sense to me. Only the first ITEM made
it into the arrays, and all the other tags somehow got counted as
ITEMS. Why?
If I simply want my XML code turned into a two dimensional array, what
is the simplest way to do it? Is xml_parse_into_ struct working
correctly, and I simply misunderstand it, or is there another way for
me to do this?
Index array
Array
(
[ITEM] => Array
(
[0] => 0
[1] => 2
[2] => 3
[3] => 5
[4] => 6
[5] => 8
[6] => 9
)
[CBID] => Array
(
[0] => 1
)
[CBHEADLINE] => Array
(
[0] => 4
)
[CBMAINCONTENT] => Array
(
[0] => 7
)
)
Vals array
Array
(
[0] => Array
(
[tag] => ITEM
[type] => open
[level] => 1
[value] =>
)
[1] => Array
(
[tag] => CBID
[type] => complete
[level] => 2
[value] => 345
)
[2] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)
[3] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)
[4] => Array
(
[tag] => CBHEADLINE
[type] => complete
[level] => 2
[value] => Hey there
)
[5] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)
[6] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)
[7] => Array
(
[tag] => CBMAINCONTENT
[type] => complete
[level] => 2
[value] => God is not dead, he is merely sleeping.
)
[8] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)
[9] => Array
(
[tag] => ITEM
[type] => close
[level] => 1
)
)
www.php.net and put this code up on a site:
<?php
$simple = <<<END
<item>
<cbId>345</cbId>
<cbHeadline>H ey there</cbHeadline>
<cbMainContent> God is not dead, he is merely
sleeping.</cbMainContent>
</item>
<item>
<cbId>432</cbId>
<cbHeadline>Wha t a dog, what a dog</cbHeadline>
<cbMainContent> In the manger.</cbMainContent>
</item>
END;
$p = xml_parser_crea te();
xml_parse_into_ struct($p, $simple, $vals, $index);
xml_parser_free ($p);
echo "Index array\n";
print_r($index) ;
echo "\nVals array\n";
print_r($vals);
?>
However, what I got back made no sense to me. Only the first ITEM made
it into the arrays, and all the other tags somehow got counted as
ITEMS. Why?
If I simply want my XML code turned into a two dimensional array, what
is the simplest way to do it? Is xml_parse_into_ struct working
correctly, and I simply misunderstand it, or is there another way for
me to do this?
Index array
Array
(
[ITEM] => Array
(
[0] => 0
[1] => 2
[2] => 3
[3] => 5
[4] => 6
[5] => 8
[6] => 9
)
[CBID] => Array
(
[0] => 1
)
[CBHEADLINE] => Array
(
[0] => 4
)
[CBMAINCONTENT] => Array
(
[0] => 7
)
)
Vals array
Array
(
[0] => Array
(
[tag] => ITEM
[type] => open
[level] => 1
[value] =>
)
[1] => Array
(
[tag] => CBID
[type] => complete
[level] => 2
[value] => 345
)
[2] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)
[3] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)
[4] => Array
(
[tag] => CBHEADLINE
[type] => complete
[level] => 2
[value] => Hey there
)
[5] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)
[6] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)
[7] => Array
(
[tag] => CBMAINCONTENT
[type] => complete
[level] => 2
[value] => God is not dead, he is merely sleeping.
)
[8] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)
[9] => Array
(
[tag] => ITEM
[type] => close
[level] => 1
)
)
Comment