Page 1 of 1

get xml attributes... Help !

Posted: Mon Nov 22, 2004 8:42 am
by idas
Hi all,

i have a problem with parse all xml attributes from external my_file.xml
to two arrays.
Can anybody help me with this ??? plz

I need two arrays like this:
Array names
(
[0] => apple
[1] => microsoft
[2] => sun
)

Array links
(
[0] => apple.com
[1] => microsoft.com
[2] => sun.com
)

my_file.xml
<?xml version="1.0"?>
<all>
<data name="apple" link="apple.com"></data>
<data name="microsoft" link="microsoft.com"></data>
<data name="sun" link="sun.com"></data>
</all>

<?
if (file_exists("my_file.xml")) {
$xml = simplexml_load_file("my_file.xml");
foreach($xml->data[0]->attributes() as $values) {
$links[] = $values;
}
}

Thanks
idas

Posted: Mon Nov 22, 2004 12:03 pm
by timvw
I don't know simple_xml, but i would do it something like:

Code: Select all

$data = "whatever xml";


$names = array();
$links = array();

// callback function when a tag is opened
function startElement($parser, $name, $attrs)
{
  global $names;
  global $links;

  $names[] = $attrs['name'];
  $links[] = $attr['link'];
}

// callback function when a tag is closed
function endElement($parser, $name) {}

// callback function when reading data between opening and closing tag
function inElement($parser, $data) {}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "inElement");
xml_parse($xml_parser, $data);
xml_parser_free($xml_parser);