Php 正在simplexml\u加载\u字符串中获取属性
下面是我用来将xml转换为数组的函数Php 正在simplexml\u加载\u字符串中获取属性,php,xml,simplexml,Php,Xml,Simplexml,下面是我用来将xml转换为数组的函数 $xml = '<xml><CodeshareInfo OperatingCarrier="EY" OperatingFlightNumber="269">ETIHAD AIRWAYS</CodeshareInfo></xml>'; $obj = simplexml_load_string($xml); // Parse XML $obj->registerXPathNames
$xml = '<xml><CodeshareInfo OperatingCarrier="EY" OperatingFlightNumber="269">ETIHAD AIRWAYS</CodeshareInfo></xml>';
$obj = simplexml_load_string($xml); // Parse XML
$obj->registerXPathNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
$array = json_decode(json_encode($obj), true); // Convert to array
但是如果我尝试不使用父节点“xml”
当我的xml数据来自soap请求时,我应该在代码中更改哪些内容以获得带有属性和值的输出,一旦收到,我将转换为数组以访问其值和属性。给定所提供的xml,您可以对其进行迭代并提取属性和值
<?php
$xml = '<xml><CodeshareInfo OperatingCarrier="EY" OperatingFlightNumber="269">ETIHAD AIRWAYS</CodeshareInfo></xml>';
$obj = simplexml_load_string($xml); // Parse XML
$obj->registerXPathNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
foreach($obj as $ob) {
echo $ob['OperatingCarrier'] . "\n";
echo $ob . "\n";
echo $ob['OperatingFlightNumber'] . "\n";
}
你就不能迭代一下$obj
?如果是几行xml,我就可以这么做了。。。但我要传递给simplexml对象的是一个根据请求生成的大xml文件。。。让我清楚地告诉你我在做什么。。它是一个旅游门户,我通过soap请求以xml的形式获取航班可用性,我将其传递给simplexml\u load\u string并将其转换为数组。。我在xml中提到的是一段巨大xml中的字符串,这是我无法获取属性的地方,我只能获取值……无论大小,你都应该能够迭代。我会提供一个样本给你我的意思…非常感谢你。。。它的工作。。。我认为这行有问题,我将$obj转换为数组$array=json\u decode(json\u encode($obj),true)代码>
Array
(
[CodeshareInfo] => ETIHAD AIRWAYS
)
<CodeshareInfo OperatingCarrier="EY" OperatingFlightNumber="269">ETIHAD AIRWAYS</CodeshareInfo>
Array
(
[@attributes] => Array
(
[OperatingCarrier] => EY
[OperatingFlightNumber] => 269
)
[0] => ETIHAD AIRWAYS
)
<?php
$xml = '<xml><CodeshareInfo OperatingCarrier="EY" OperatingFlightNumber="269">ETIHAD AIRWAYS</CodeshareInfo></xml>';
$obj = simplexml_load_string($xml); // Parse XML
$obj->registerXPathNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
foreach($obj as $ob) {
echo $ob['OperatingCarrier'] . "\n";
echo $ob . "\n";
echo $ob['OperatingFlightNumber'] . "\n";
}