Hi all,
i have a problem to convert XML data structure into php arays with the concept of SAX. I realized this has to be done using xml_parse_into_struct.But, I'm confused about the usage of this function.
Here is the xml document:
<?xml version="1.0"?>
<bookmarks category="News">
<link id="15696">
<title>CNN</title>
<url>http://www.cnn.com/</url>
<last_access>2000-09-08</last_access>
</link>
<link id="3763">
<title>Freshmeat</title>
<url>http://www.freshmeat.net/</url>
<last_access>2001-04-23</last_access>
</link>
<link id="84574">
<title>Slashdot</title>
<url>http://www.slashdot.com/</url>
<last_access>2001-12-30</last_access>
</link>
</bookmarks>
I want to change it into something like:
First Array
(
[0] => Array
(
[tag] => BOOKMARKS
[type] => open
[level] => 1
[attributes] => Array
(
[CATEGORY] => News
)
)
[1] => Array
(
[tag] => LINK
[type] => open
[level] => 2
[attributes] => Array
(
[ID] => 15696
)
)
And so on.
And second array should print :
Second Array
(
[BOOKMARKS] => Array
(
[0] => 0
[1] => 16
)
[LINK] => Array
(
[0] => 1
[1] => 5
[2] => 6
[3] => 10
[4] => 11
[5] => 15
)
[TITLE] => Array
(
[0] => 2
[1] => 7
[2] => 12
)
And so on.
Thus the first array prints all the elements or attributes it encounters and
second array prints the occurrence indicator and frequency of occurrence
of the elements.
So i wrote :
<?php
if($argc!=2)
die("\nSyntax: <Input file>\n");
$InputFileName = $argv[1];
if (!($fp = fopen($InputFileName, "r")))
die("\nFailed while opening XML input file ".$InputFileName."\n");
// Create the parser and specify the handlers
$xml_parser = xml_parser_create();
while ($xml_data = fread($fp, 4096))
{
xml_parse_into_struct($xml_parser,$xml_data,$vals,$index);
}
// PHP-SAX performs case folding by default - let's switch this off
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,false);
// Free the parser
xml_parser_free($xml_parser);
echo "Vals Array\n";
print_r($vals);
echo "\nindex array \n";
print_r($index);
?>
Is it correct?
And How if i want to enhance that program to create a third array that
contains only the URLs from each link?
So i want to take ONLY the value of URL tag.Could some one help me??
Thank you
-Stella-