Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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_Simplexml - Fatal编程技术网

使用php获取xml节点值的步骤

使用php获取xml节点值的步骤,php,xml,simplexml,Php,Xml,Simplexml,我想获取我编写的以下代码的xml节点中的值。 代码的目标是 1.Board value, 2.Address->City value, 3.Photo->PropertyPhoto->SequenceId value. 代码是: echo$fsp的输出为: <?xml version="1.0" encoding="UTF-8"?> <PropertyDetails ID="13

我想获取我编写的以下代码的xml节点中的值。

代码的目标是

        1.Board value,
        2.Address->City value,
        3.Photo->PropertyPhoto->SequenceId value.

代码是:

echo$fsp的输出为:

        <?xml version="1.0" encoding="UTF-8"?>
       <PropertyDetails ID="13953882" LastUpdated="Thu, 09 Jan 2014 01:43:48 GMT">
       <ListingID>188691</ListingID>
       <Board>117</Board>
        <Address>
             <StreetAddress>B LOCAL RD</StreetAddress>
             <AddressLine1>B LOCAL RD</AddressLine1>
             <City>PORT REXTON</City>
             <Province>Newfoundland &amp; Labrador</Province>
             <PostalCode>A0C2H0</PostalCode>
             <Country>Canada</Country>
        </Address>
        <Photo>
           <PropertyPhoto>
               <SequenceId>1</SequenceId>
            </PropertyPhoto>

           <PropertyPhoto>
               <SequenceId>12</SequenceId>
            </PropertyPhoto>

        </Photo>
        <ViewType>Ocean view</ViewType>

帮助我。

首先从变量中的字符串加载xml:

$doc = new DOMDocument();
$doc->load($fsp);
apidoc在此:

然后读取所需的标签:

//board
$boards = $doc->getElementsByTagName('Board');
echo $boards[0]->nodeValue, PHP_EOL;
//city
$cities = $doc->getElementsByTagName('City');
echo $cities[0]->nodeValue, PHP_EOL;
//sequence ids
$arrIds = array();
foreach ($doc->getElementsByTagName('SequenceId') as $sid) $arrIds[] = $sid->nodeValue;
echo implode(',', $arrIds);
这假设您的文件中只有一个列表!否则,您必须根据父节点(而不是全局文档)进行标记查找


希望这有帮助

我在这里看到了几件事。首先,您的XML格式不正确。您的PropertyDetails标记未关闭。因此,请关闭它,并使用SimpleXMLElement类,而不是使用DOMDocument

首先,您的XML需要进行如下更正:

   <?xml version="1.0" encoding="UTF-8"?>
   <PropertyDetails ID="13953882" LastUpdated="Thu, 09 Jan 2014 01:43:48 GMT">
      <ListingID>188691</ListingID>
      <Board>117</Board>
      <Address>
         <StreetAddress>B LOCAL RD</StreetAddress>
         <AddressLine1>B LOCAL RD</AddressLine1>
         <City>PORT REXTON</City>
         <Province>Newfoundland &amp; Labrador</Province>
         <PostalCode>A0C2H0</PostalCode>
         <Country>Canada</Country>
      </Address>
      <Photo>
       <PropertyPhoto>
           <SequenceId>1</SequenceId>
        </PropertyPhoto>

       <PropertyPhoto>
           <SequenceId>12</SequenceId>
        </PropertyPhoto>

      </Photo>
      <ViewType>Ocean view</ViewType>
    </PropertyDetails>
因为是XML中的根节点,所以不需要通过->PropertyDetails[0]访问它。它知道

希望这有帮助

//board
$boards = $doc->getElementsByTagName('Board');
echo $boards[0]->nodeValue, PHP_EOL;
//city
$cities = $doc->getElementsByTagName('City');
echo $cities[0]->nodeValue, PHP_EOL;
//sequence ids
$arrIds = array();
foreach ($doc->getElementsByTagName('SequenceId') as $sid) $arrIds[] = $sid->nodeValue;
echo implode(',', $arrIds);
   <?xml version="1.0" encoding="UTF-8"?>
   <PropertyDetails ID="13953882" LastUpdated="Thu, 09 Jan 2014 01:43:48 GMT">
      <ListingID>188691</ListingID>
      <Board>117</Board>
      <Address>
         <StreetAddress>B LOCAL RD</StreetAddress>
         <AddressLine1>B LOCAL RD</AddressLine1>
         <City>PORT REXTON</City>
         <Province>Newfoundland &amp; Labrador</Province>
         <PostalCode>A0C2H0</PostalCode>
         <Country>Canada</Country>
      </Address>
      <Photo>
       <PropertyPhoto>
           <SequenceId>1</SequenceId>
        </PropertyPhoto>

       <PropertyPhoto>
           <SequenceId>12</SequenceId>
        </PropertyPhoto>

      </Photo>
      <ViewType>Ocean view</ViewType>
    </PropertyDetails>
$s = new SimpleXMLElement($fsp);
echo $s->Board;
echo $s->Address->City;
$sequence_ids = array();
foreach($s->Photo->PropertyPhoto as $PropertyPhoto) 
    $sequence_ids[] = $PropertyPhoto->SequenceId;
echo implode(',',$sequence_ids);