2
<?xml version="1.0" encoding="utf-8"?>
<mainXML>
    <items>
        <item category="Dekorationer" name="Flot væg" description="Meget flot væg. Passer alle stuer." price="149" />
        <item category="Fritid" name="Fodbold" description="Meget rund bold. Rørt af messi." price="600" />
    </items>
</mainXML>

How can i read this?

So i can make like a php loop that outputs the category, name and description for example?

I tried and started out with this:

    $doc = new DOMDocument();
    $doc->load( 'ex.xml' );

    $items = $doc->getElementsByTagName( "item" );
    foreach( $items as $item )
    {
        $categorys = $item->getElementsByTagName( "category" );
        $category = $categorys->item(0)->nodeValue;

        echo $category . " -- ";
    }
3
  • 1
    category is an attribute (not a tag). See XML. To obtain it via the DOMElement, use getAttribute(). Commented Dec 12, 2011 at 22:50
  • @hakre thank you that worked. Please write it as an answer Commented Dec 12, 2011 at 22:54
  • Sure, I also added some example code ;). Commented Dec 12, 2011 at 22:55

3 Answers 3

2

category is an attribute (not a tag). See XMLWikipedia. To obtain it via the DOMElement, use getAttribute()Docs:

foreach ($items as $item)
{
    $category = $item->getAttribute('category');
    echo $category, ' -- ';
}

Same for description, just change the name of the attribute to obtain:

foreach ($items as $item)
{
    echo 'Category: ', $item->getAttribute("category"), "\n",
         'Description: ', $item->getAttribute("description"), ' -- ';
}
Sign up to request clarification or add additional context in comments.

Comments

2

I'd recommend PHP's simplexml_load_file()

$xml = simplexml_load_file($xmlFile);
foreach ($xml->items->item as $item) {
    echo $item['category'] . ", " . $item['name'] . ", " . $item['description'] . "\n";
}

UPDATED, missed the extra tag.

4 Comments

Good idea, poor implementation. It doesn't work.
@nickb, Thank you for catching that.
You would have had my upvote if you wouldn't load the file again but get the SimpleXMLElement by using the DOMDocument from the question. Both can work together, see simplexml_import_dom.
@hakre, Thanks, I didn't know you could do that. Looks like I've got some reading to do tonight.
1

Here an example using PHP's SimpleXML, specifically the simplexml_load_string function.

$xml = '<?xml version="1.0" encoding="utf-8"?>
<mainXML>
    <items>
        <item category="Dekorationer" name="Flot væg" description="Meget flot væg. Passer alle stuer." price="149" />
        <item category="Fritid" name="Fodbold" description="Meget rund bold. Rørt af messi." price="600" />
    </items>
</mainXML>';

 $xml = simplexml_load_string( $xml);

 foreach( $xml->items[0] as $item)
 {
     $attributes = $item[0]->attributes();
     echo 'Category: ' . $attributes['category'] . ', Name: ' . $attributes['name'] . ', Description: ' . $attributes['description'];
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.