简体   繁体   中英

php: how to read xml element with namespace?

I am using php on xamp latest version (downloaded today). I have a php page where I would like to read xml element with a namespace. How can I do that?

Here is a example of an xml object:

$obj = new SimpleXMLElement('<root xmlns:np2="http://test.com">
    <np2:a>test1</np2:a>
    <np2:b>test2</np2:b>
</root>');

When using SimpleXML you have to use the children($namespace) method.

<?php
$obj = new SimpleXMLElement('<root xmlns:np2="http://test.com">
    <np2:a>test1</np2:a>
    <np2:b>test2</np2:b>
</root>');

foreach( $obj->children('http://test.com') as $c ) {
    echo $c, "\r\n";
}

see

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM