Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP读取XML属性_Php_Xml_Soap_Simplexml - Fatal编程技术网

使用PHP读取XML属性

使用PHP读取XML属性,php,xml,soap,simplexml,Php,Xml,Soap,Simplexml,我正在使用一个API来显示搜索查询的结果 输出将是XML的形式,我需要解析XML并在浏览器中显示结果 该文件包含许多属性,我不确定如何从中提取结果 这是我的密码: <?xml version="1.0" ?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns3:tourDetailsFullResponse xmlns:ns2="http://exa

我正在使用一个API来显示搜索查询的结果

输出将是XML的形式,我需要解析XML并在浏览器中显示结果

该文件包含许多属性,我不确定如何从中提取结果

这是我的密码:

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns3:tourDetailsFullResponse xmlns:ns2="http://example.com/" xmlns:ns3="http://example.com/" xmlns:ns4="http://www.example.com/">

<ns4:MarketLocalisedTourData SellingCompanyCode="CODE1" OperatingProductCode="CODE2" MarketVariationCode="CODE3" Duration="8" Currency="USD" CatalogueCode="CODE4" TropicsBrochureCode="CODE5" BrandCode="CODE6" BookableOnline="true">

<ns4:TourInfo>
<ns4:TourName>London to Rome Highlights 2013</ns4:TourName>
<ns4:Metadata>
    <ns4:TourCategories>
        <ns4:TourCategory Name="TourStyles">
            <ns4:CategoryValue Name="European Discoveries"/>
        </ns4:TourCategory>
    </ns4:TourCategories>
    <ns4:Brochures>
        <ns4:Brochure Name="CostSaver 2013" Code="CODE1"/>
    </ns4:Brochures>
</ns4:Metadata>
<ns4:ContinentsVisited>
    <ns4:Continent Name="Europe" Code="EURO"/>
</ns4:ContinentsVisited>
<ns4:CountriesVisited>
    <ns4:Country Name="France" Continent="EURO" Code="FR"/>
    <ns4:Country Name="United Kingdom" Continent="EURO" Code="GB"/>
    <ns4:Country Name="Italy" Continent="EURO" Code="IT"/>
    <ns4:Country Name="Switzerland" Continent="EURO" Code="CH"/>
</ns4:CountriesVisited>
<ns4:LocationsVisited>
    <ns4:Location Name="Paris" Country="FR"/>
    <ns4:Location Name="Dover" Country="GB"/>
    <ns4:Location Name="London" Country="GB"/>
    <ns4:Location Name="Rome" Country="IT"/>
    <ns4:Location Name="Calais" Country="FR"/>
    <ns4:Location Name="Venice" Country="IT"/>
    <ns4:Location Name="Lucerne" Country="CH"/>
    <ns4:Location Name="Florence" Country="IT"/>
</ns4:LocationsVisited>
<ns4:Description>A lively journey through elegant Paris, gorgeous Lake Lucerne, charming Venice and cultural Florence, bookended by stays in London and legendary Rome. Spend a day at leisure in London and get the best views of Rome from high up in St. Peter’s Basilica before throwing a lucky coin in Trevi Fountain.</ns4:Description>
<ns4:Assets>
    <ns4:Image Width="297" Url="http://www.example.com/imageurl" Type="map" Name="route_map" Height="297" Caption="route_map"/>
    <ns4:Image Width="600" Url="http://www.example.com/imageurl" Type="map" Name="route_map" Height="600" Caption="route_map"/>
    <ns4:Image Width="531" Url="http://www.example.com/imageurl" Type="photo" Name="primary_image" Height="531" Caption="primary_image"/>
    <ns4:Image Width="125" Url="http://www.example.com/imageurl" Type="photo" Name="primary_image" Height="125" Caption="primary_image"/>
</ns4:Assets>
  ....continues
还尝试了导致相同错误的所有其他可能性

致命错误:在C:\xampp\htdocs\test\xmlread2.php的第10行对非对象调用成员函数attributes()


我已经为此搜索了几天多的解决方案。

如果您希望遍历到其自己名称空间中的子级,则必须告诉SimpleXMLElement哪个名称空间:

$ns4 = $xml->children('S', true)->Body
        ->children('ns3', true)->tourDetailsFullResponse
            ->children('ns4', true);

$locations = $ns4->MarketLocalisedTourData->TourInfo->LocationsVisited->Location;

foreach ($locations as $location) {
    $attributes = $location->attributes();
    echo $attributes['Name'];
    echo $attributes['Country'];
}

但是,您可以通过使用使您的生活更加轻松。

如果您想遍历到其自己名称空间中的子级,则必须告诉SimpleXMLElement哪个名称空间:

$ns4 = $xml->children('S', true)->Body
        ->children('ns3', true)->tourDetailsFullResponse
            ->children('ns4', true);

$locations = $ns4->MarketLocalisedTourData->TourInfo->LocationsVisited->Location;

foreach ($locations as $location) {
    $attributes = $location->attributes();
    echo $attributes['Name'];
    echo $attributes['Country'];
}

但是,您可以通过使用使您的生活更加轻松。

美元xml有什么?你能把结果打印出来吗。 如果它不包含任何东西

那么问题是名称空间 看看下面的问题:

$feed=“response1_full.xml”;
回声“;
$sxe=simplexml\u加载文件($feed);
如果($sxe==false){
echo“加载XML失败\n”;
foreach(libxml_get_errors()作为$error){
回显“\t”,$error->message;
}
}
如果(文件_存在('response1_full.xml')){
$xml=simplexml_load_文件('response1_full.xml');
//打印(xml);
}否则{
退出('打开test.xml失败');
}
打印($sxe->Body->tourDetailsFullResponse->MarketLocalisedTourData->TourInfo->LocationsVisited->Location[0]);
打印($sxe->Body->tourDetailsFullResponse->MarketLocalisedTourData->TourInfo->LocationsVisited->Location[1]);

美元xml有什么功能?你能把结果打印出来吗。 如果它不包含任何东西

那么问题是名称空间 看看下面的问题:

$feed=“response1_full.xml”;
回声“;
$sxe=simplexml\u加载文件($feed);
如果($sxe==false){
echo“加载XML失败\n”;
foreach(libxml_get_errors()作为$error){
回显“\t”,$error->message;
}
}
如果(文件_存在('response1_full.xml')){
$xml=simplexml_load_文件('response1_full.xml');
//打印(xml);
}否则{
退出('打开test.xml失败');
}
打印($sxe->Body->tourDetailsFullResponse->MarketLocalisedTourData->TourInfo->LocationsVisited->Location[0]);
打印($sxe->Body->tourDetailsFullResponse->MarketLocalisedTourData->TourInfo->LocationsVisited->Location[1]);

最后我通过DOM找到了答案

@Deepak Srinivasan:我尝试了您的建议,在执行之前注册名称空间,但我没有忘记,可能b我不确定如何继续

@m4t1t0:感谢您的回复

我已经粘贴了完整文件的pastebin url,你们都可以从下面答案指定的url中看到该文件

我使用getAttribute方法得到了解决方案

<ns4:WhatsIncluded>
$doc=newDOMDocument();
$doc->load('response1_full.xml');
//图名
$tname=$doc->getElementsByTagName(“TourName”);
$tourname=$tname->item(0)->nodeValue;
echo$tourname;
//访问的地点
$locations=$doc->getElementsByTagName(“位置”);
foreach($locations作为$location)
{
$locationname=$location->getAttribute('Name');
$locationcountry=$location->getAttribute('Country');
echo$locationname.'-.$locationcountry.
; }
准确打印出所需的详细信息

但现在我在同一份文件中有一个小小的疑问, 我有一些嵌套格式的信息,是否有任何方法可以轻松地提取它

这是我的档案

在文件中可以看到一个部分


其中包含标题和文本(ul-li格式)

有没有什么办法可以把数据放在这张桌子里。 如果我从部分下的文本中提取数据,我会将li项完全作为一个段落

但是当我在文本中使用foreach时,我只得到第一个li项

我也尝试了foreach的Textul,但仍然只有一个结果

有什么建议吗


非常感谢。

最后我通过DOM找到了答案

@Deepak Srinivasan:我尝试了您的建议,在执行之前注册名称空间,但我没有忘记,可能b我不确定如何继续

@m4t1t0:感谢您的回复

我已经粘贴了完整文件的pastebin url,你们都可以从下面答案指定的url中看到该文件

我使用getAttribute方法得到了解决方案

<ns4:WhatsIncluded>
$doc=newDOMDocument();
$doc->load('response1_full.xml');
//图名
$tname=$doc->getElementsByTagName(“TourName”);
$tourname=$tname->item(0)->nodeValue;
echo$tourname;
//访问的地点
$locations=$doc->getElementsByTagName(“位置”);
foreach($locations作为$location)
{
$locationname=$location->getAttribute('Name');
$locationcountry=$location->getAttribute('Country');
echo$locationname.'-.$locationcountry.
; }
准确打印出所需的详细信息

但现在我在同一份文件中有一个小小的疑问, 我有一些嵌套格式的信息,是否有任何方法可以轻松地提取它

这是我的档案

在文件中可以看到一个部分


其中包含标题和文本(ul-li格式)

有没有什么办法可以把数据放在这张桌子里。 如果我从部分下的文本中提取数据,我会将li项完全作为一个段落

但是当我在文本中使用foreach时,我只得到第一个li项

我也尝试了foreach的Textul,但仍然只有一个结果

有什么建议吗


非常感谢。

如果我打印$xml,我会得到以下答案:
simplexmlement Object()
我已经将文件保存在名称下,为什么要将文件作为字符串加载并保存
<ns4:WhatsIncluded>