0

I cannot get the attribute value of the xml element becaue everytime i click the link there is no value in id= but when view my xml there is a attribute value, why is this happening am i missing something?

Here is my PHP code:

 <?php
function parse_recursive(SimpleXMLElement $element, $level = 0)
{
    $indent = str_repeat('', $level); 

    $value = trim((string) $element); 
    $children = $element->children(); 
    $attributes = $element->attributes();

    echo '<ul>';
    if(count($children) == 0 && !empty($value))
    {   
        if($element->getName() == 'GroupName'){
            echo '<li><a href="http://localhost:8080/test/test.php?id=';
            if(count($attributes) > 0)
            {
                foreach($attributes as $attribute)
                {
                    if($attribute->getName() == 'Id'){
                        echo $attribute;
                    }
                }
            }
            echo '">'.$element.'</a></li>';
        }
    }   

    if(count($children))
    {
        foreach($children as $child)
        {
            parse_recursive($child, $level+1);
        }
    }
    echo '</ul>';
}

$xml = new SimpleXMLElement('main.xml', null, true);
parse_recursive($xml);
?>

Here is my xml structure:

enter image description here

1 Answer 1

1

It appears that your code will only output anything for GroupName elements:

if($element->getName() == 'GroupName'){

and it will only output the Id attributes of GroupName elements.

The single GroupName in your source XML doesn't have an Id attribute.

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

2 Comments

i see so the problem is the xml format?, i also create the xml using c# code,
I'm not sure. It just depends on what you are trying to do. Could you provide a sample of what the expected result is?

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.