search

Home  >  Q&A  >  body text

How does php parse xml tags that have both values ​​and attributes?

use:

$xml   = simplexml_load_string($content);
$array = json_decode(json_encode((array) $xml), 1);

When parsing XML into an array in this way,
For:

<a name="aaaa">
    <b></b>
</a>    

and

<a>content</a> 

can be parsed correctly, but for this:

<a name="aaaa">content</a> 

You cannot get the name attribute, but you can only get the content content.

Is there any good way to deal with this problem?

巴扎黑巴扎黑2754 days ago773

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-14 10:52:11

    $xml = simplexml_load_string($content);
    foreach($xml->attributes() AS $a => $b) {
    
    echo "$a = $b <br />";
    }

    reply
    0
  • Cancelreply