Page 1 of 1

Parsing XML.

Posted: Mon Aug 13, 2007 1:12 pm
by 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>
These are the methods I've tried:[/syntax]

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>");
}
Here's the results:

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 6
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]

Posted: Mon Aug 13, 2007 1:24 pm
by volka
A SimpleXMLElement object is not an array. But you can use foreach on it.
e.g.

Code: Select all

<?php
$s = simplexml_load_file("assessment.xml");

foreach( $s->object as $object) {
	echo '<tr><th>object ', $object['title'], '</th>';
	foreach( $object->param as $param) {
		echo '<td>', $param, '</td>';
	}
	echo "</tr>\n";
}

Posted: Mon Aug 13, 2007 1:33 pm
by Krista
Okay, that returned:
object Sleep text text text

But how can I get it to return the param name="1" and value="1"
It's returning the text between the tags, find, but these are sort of like surveys. The param name is supposed to be the question number, and the param value is the answer (numeric equivalent) and the "title" is the question, in text, if they choose to send that.

Krista

Posted: Mon Aug 13, 2007 1:54 pm
by Krista
Okay this worked:

Code: Select all

echo '<td>', $param, $param['name'], $param['value'], '</td>';
The problem is that I'm not sure WHY it worked ;) Why is $param only ever referring to what's between the tags <param> </param> Or should I just not worry about it and be happy I can get the values out? ;)

Krista

Posted: Tue Aug 14, 2007 5:24 am
by volka