Parsing XML.
Posted: Mon Aug 13, 2007 1:12 pm
feyd | Please use
These are the methods I've tried:[/syntax]
Here's the results:
Ideas? What am I doing wrong?
Krista
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I've been given what looks like a pretty basic xml format to parse out in order to get the values into arrays that PHP can use. For some reason, both SimpleXML and expat are refusing to give me the param name and value's
This is the XML:
[syntax="xml"]<message>
<object title="AssessmentOne" ver="1">
<param name="1" value="1">text</param>
<param name="2" value="2">text</param>
<param name="3" value="3">text</param>
</object>
</message>
Code: Select all
$xmlFileData = file_get_contents("assessment.xml");
//Here's our Simple XML parser!
$xmlData = new SimpleXMLElement($xmlFileData);
//And here's the output.
print_r($xmlData);Code: Select all
//create expat parser
if(!($parser = xml_parser_create()))
{
print("Could not create parser!<br>");
exit();
}
//get entire file
$data = file_get_contents("assessment.xml");
//parse file into array
xml_parse_into_struct($parser, $data, $structure, $index);
//destroy parser
xml_parser_free($parser);
print("Structure:<br>" .
"<table border=\"1\">" .
"<tr>" .
"<th>tag</th>" .
"<th>type</th>" .
"<th>level</th>" .
"<th>value</th>" .
"<tr>");
foreach($structure as $s)
{
if(!isset($s["value"]))
{
$s["value"] = "";
}
print("<tr>" .
"<td>{$s["tag"]}</td>" .
"<td>{$s["type"]}</td>" .
"<td>{$s["level"]}</td>" .
"<td>{$s["value"]}</td>" .
"<tr>");
}
print("</table>");
print("Element Reference:<br>");
foreach($index as $key=>$value)
{
print("$key:");
foreach($value as $i)
{
print(" $i");
}
print("<br>");
}Code: Select all
SimpleXMLElement Object ( [object] => SimpleXMLElement Object ( [@attributes] => Array ( [title] => Sleep [ver] => 1 ) [param] => Array ( [0] => text [1] => text [2] => text ) ) )
Structure:
tag type level value
MESSAGE open 1
OBJECT open 2
PARAM complete 3 text
OBJECT cdata 2
PARAM complete 3 text
OBJECT cdata 2
PARAM complete 3 text
OBJECT cdata 2
OBJECT close 2
MESSAGE cdata 1
MESSAGE close 1
Element Reference:
MESSAGE: 0 9 10
OBJECT: 1 3 5 7 8
PARAM: 2 4 6Krista
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]