使用SimpleXMLElement将值从XML名称空间传递到PHP变量
我使用SimpleXMLElement创建了XML内容,并显示了var_转储结果 示例代码:使用SimpleXMLElement将值从XML名称空间传递到PHP变量,php,xml,var-dump,Php,Xml,Var Dump,我使用SimpleXMLElement创建了XML内容,并显示了var_转储结果 示例代码: $data = new SimpleXMLElement($xml); $data->registerXPathNamespace('ns','http://endpoint.websitecom/'); $part = $data->xpath("//ns:return"); var_dump($part[0]->children("ns",true)); 我的结果: object
$data = new SimpleXMLElement($xml);
$data->registerXPathNamespace('ns','http://endpoint.websitecom/');
$part = $data->xpath("//ns:return");
var_dump($part[0]->children("ns",true));
我的结果:
object(SimpleXMLElement)[4]
public 'result' =>
object(SimpleXMLElement)[6]
public 'result' =>
object(SimpleXMLElement)[10]
public 'Code' => string '0' (length=1)
public 'amount' => string '2020.03' (length=7)
public 'code2' => string '3934.8' (length=6)
object(SimpleXMLElement)[8]
如何将amount、code2变量传递到PHP变量或数组中
$amount = ?????
$code2 = ????
我用流动的xml代码编写了一个测试用例(在您的var_转储上反向工程):
您可以添加xml结构吗?我不确定我是否答对了。永远不要因为到目前为止你还没有得到任何答案而重复你自己的问题。这只会让你落选,你会失去潜在的朋友。平息许多我没有注意到的疑惑。我找到了两个@重复材料:-也是自复印机。
<unknown xmlns:ns="http://endpoint.websitecom/">
<ns:return>
<ns:result>
<ns:result>
<ns:Code>0</ns:Code>
</ns:result>
</ns:result>
<ns:amount>2020.03</ns:amount>
<ns:code2>3934.8</ns:code2>
</ns:return>
</unknown>
$data = new SimpleXMLElement($xml);
$data->registerXPathNamespace('ns','http://endpoint.websitecom/');
$part = $data->xpath("//ns:return");
$branch=$part[0]->children("ns",true);
var_dump($branch); // your current output
$amount=$branch->amount;
$code2 =$branch->code2;