简体   繁体   中英

Remove xml namespaces in php

I am trying to remove the xml namespaces using php, however I am having difficulty in achieving this.

For example, I would like the following xml code response from a remote server

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <data>
            <id>1</id>
            <id>2</id>
            <id>3</id>
        </data>
    </soap:Body>
</soap:Envelope>

to be parsed on a local server to be;

<?xml version="1.0" encoding="utf-8"?>
<data>
    <id>1</id>
    <id>2</id>
    <id>3</id>
</data>

Thank you.

You can easily extract the data using XPath and SimpleXML, this code takes a string (this uses file_get_contents , I think you get it from cURL) and convert it to an XML document. Then uses XPath to extract the <soap:Body> children nodes (using //soap:Body/* ) - this will give a list of matching nodes. To output the data it just uses asXML() ...

$data = file_get_contents("data.xml");
$xml = simplexml_load_string($data);
$body = $xml->xpath("//soap:Body/*");
echo $body[0]->asXML();

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