4

I have an XML file with data stored like this:

<myxml>
<item name="column18">88744544</item>
<item name="column11">47884994</item>
<item name="column3">44788894</item>
</myxml>

I need to first check (and be sure that) column11 is defined (there is no particular order), and then get its value.

Using simple XML is not seeming to work.

I have the following, but the value is missing.

<?php
if (count($xml->myxml->item) > 0)
{
 foreach ($xml->myxml->item as $item)
 {
  var_dump($item->attributes());
 }
}
?>

$item->attributes()->column11 doesn't work.

1
  • this is because it would be if (count($xml->item) > 0) { foreach ($xml->item as $item) { var_dump($item->attributes()); } } and to access column 11 you would use $xml->item[1]->attributes(); Commented Apr 21, 2010 at 1:09

2 Answers 2

4

Dont include the opening tabs and attributes. For example:

<?php
if (count($xml->item) > 0)
{
 foreach ($xml->item as $item)
 {
  var_dump($item); //For the info
  echo $item['name']; //if you needed the name
 }
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

This works, but xpath is much easier for what I'm trying to accomplish. Thanks though.
3

Try XPath.

if ($xml->xpath('//item[@name="column11"]'))
{
    echo 'exists';
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.