2

XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<lessons>
    <lesson level="1" course="2">
                 <name type="Dog" category="Animals">Dog name</name>
          </lesson>
</lessons>

I want to get the values saved like this:

$type = "Dog";
$category = "Animals";
$name = "dog name";

This is what I've done:

    foreach($xml->name as $name){
        $type = $name['type'];
        $category = $name['category'];

        echo "Type: $type Category: $category<br>";
        // AND TO get the text, haven't figuered it out yet.. <name ..="" ..="">text</name>
    }

But it doesn't work. Don't get any errors neither any output. Any ideas?

EDIT: OK. I changed foreach($xml->name as $name)

to foreach($xml->lesson->name as $name)

so I get the values of the attribute. But now I don't know how to get the value of the children. I've tried this: $xml->lesson->children()

It prints children()

SOLVED: $text = $xml->lesson->children(); echo $text;

PROBLEM WAS: I'm using utf-8 in my other code but didn't change it.

3
  • @MuthuKumaran I think this is one of those questions that is legitimately implied. Short code sample, it doesn't output anything, what's wrong? Commented Oct 26, 2012 at 3:57
  • 1
    Probably because $xml refers to <lessons>, not <lesson>? Commented Oct 26, 2012 at 4:00
  • Might be so, and a solution would be? Commented Oct 26, 2012 at 4:04

2 Answers 2

1

Edit : this part related to a question typo. If you copied your xml directly from where you were editting it, then part of the problem might be that it is malformed. You have an opening <lessons> but you appear to wrongly try to close it with </lesson>.

Also, depending on your root node settings, ->name may or may not be a child of the $xml object. Can you post a var_dump() of it and get some clues?

Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry it's being closed with </lessons> Just I who wrote it wrong here.
@Kilise how about that var_dump()? :)
Solved it. It should be: foreach($xml->lesson->name as $name)
1

I think, there is some problem in your xml.

-> You have to close lessons tag correctly.Because you have entered </lesson> (see last line) instead of </lessons>. If you start any tag, you should use the same tag name while closing..

you can use this code to extract values from your xml,

<?php

$xmlstring='<lessons>
    <lesson level="1" course="2">
                 <name type="Dog" category="Animals">Dog name</name>
          </lesson>
</lessons>';

$xml = simplexml_load_string($xmlstring);
$ATTRIBUTE=array();
$counter = 0;
foreach($xml->children() as $key=>$child)
  {
    $counter++;
    $ATTRIBUTE[$counter]["type"]=$child->name->attributes()->type;
    $ATTRIBUTE[$counter]["category"]=$child->name->attributes()->category;
    $ATTRIBUTE[$counter]["value"]= $child->name;
  }

echo "<pre>";
print_r($ATTRIBUTE);
?>

here you will get everything in array. So you can fetch based on your requirement.

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.