替换.xml文件中的无效 URI



如何替换 xml 文件中的无效 URI,我无法解析 xml 文件,因为它在尝试解析文件时给出了无效的 URI 错误。如何将.xml文件的内容读取到字符串中,以便替换无效的 URI。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Submit xmlns="http://localhost/....">
    <Order>
    <Components>
        <Component>
            <ocode> ABC</ocode>
        </Component>
    </Components>
</Order>
</Submit>
</soap:Body>
</soap:Envelope>

如果您只需要内容,请使用 SimpleXML 和 XPath 获取正文内容...

$xml=simplexml_load_file("NewFile.xml"); 
$content = $xml->xpath("//soap:Body/*");
echo $content[0]->Order->asXML();

会给...

<Order>
    <Components>
        <Component>
            <ocode> ABC</ocode>
        </Component>
    </Components>
</Order>

不确定命名空间http://localhost/....应该是什么,但它应该是一个有效的 URI - 即使它是http://localhost .

编辑:

要尝试修复 URI,您可以先将文件读取为字符串,然后将无效字符串替换为有效字符串...

$data = file_get_contents("NewFile.xml");
$data = str_replace("http://localhost/....", "http://localhost", $data);
$xml=simplexml_load_string($data);

或者您也可以尝试删除所有属性...

$data = preg_replace("/<Submit.*?>/", "<Submit>", $data);

最新更新