首页 > 解决方案 > 有什么方法可以让文件包含“ ” 2次

问题描述

我点击了一项服务,并获得了类似以下 xml 的数据。

<?xml version='1.0'?>
<Properties>
    <Property>  
        <Prop_Class>Residential</Prop_Class>
        <Prop_RefId>Resads -  FHGS - 2034 - 160 - 067546</Prop_RefId>
        <Prop_CompanyGroup>ma</Prop_CompanyGroup>
        <Prop_CompanyName>Propertysvhavs</Prop_CompanyName>
        <Prop_Locality>30</Prop_Locality>
        <Prop_Address1>3 Bedroom houses</Prop_Address1>
        <Prop_Address2></Prop_Address2>
        <Prop_Address3>Clare Road</Prop_Address3>
        <Prop_Address4></Prop_Address4>
        <Prop_Eircode></Prop_Eircode>
        <Prop_Latitude>533.3498</Prop_Latitude >
        <Prop_Longitude>623.260300000000029</Prop_Longitude >
        <Prop_Status>A</Prop_Status> 
        <Prop_SaleOrRent>Sale</Prop_SaleOrRent>    
        <Prop_SaleType>For Sale</Prop_SaleType>         
        <Prop_Type>Residential   Apartment</Prop_Type>               
        <Prop_Bedrooms>1</Prop_Bedrooms>
        <Prop_Bathrooms>3</Prop_Bathrooms>
        <Prop_FullDescription></Prop_FullDescription> 
        <Prop_Price></Prop_Price>
        <Prop_PriceOption>m</Prop_PriceOption> 
        <Prop_ShowPrice>Y</Prop_ShowPrice>
        <Prop_Negotiator>Philip O'Reilly - Test </Prop_Negotiator>                              
        <Prop_EnergyRating>A2</Prop_EnergyRating>
        <Prop_EnergyRatingDetails>A2</Prop_EnergyRatingDetails>
    </Property>
</Properties>
<?xml version='1.0'?>
<Images>
    <Image>
        <Prim_RefId>Resads -  FHGS - 2034 - 160 - 067546</Prim_RefId>
        <Prim_CompanyGroup>ma</Prim_CompanyGroup>
        <Prim_Type>PA</Prim_Type>
        <Prim_Filename>http://www.prhjsgdh.ie/uploads/web/286_3 bed dev.jpg</Prim_Filename>
        <Prim_Status>A</Prim_Status>
        <Prim_Class>Residential</Prim_Class>
    </Image>
    <Image>
        <Prim_RefId>Resads -  FHGS - 2034 - 160 - 067546</Prim_RefId>
        <Prim_CompanyGroup>ma</Prim_CompanyGroup>
        <Prim_Type>PA</Prim_Type>
        <Prim_Filename>http://www.hashjshd.ie/uploads/web/286_3bedsemi-2014.jpg</Prim_Filename>
        <Prim_Status>A</Prim_Status>
        <Prim_Class>Residential</Prim_Class>
    </Image>
    <Image>
        <Prim_RefId>Resads -  FHGS - 2034 - 160 - 067546</Prim_RefId>
        <Prim_CompanyGroup>ma</Prim_CompanyGroup>
        <Prim_Type>PA</Prim_Type>
        <Prim_Filename>http://www.asdbjhsdh.ie/uploads/web/286_3 bed dev.jpg</Prim_Filename>
        <Prim_Status>A</Prim_Status>
        <Prim_Class>Residential</Prim_Class>
    </Image>
</Images>

但是这个 XML 包含 2 <?xml version='1.0'?>。这样我就无法获取文件内容。

有没有办法将文件作为 XML 获取?甚至有什么方法可以将文件拆分为出现的次数<?xml version='1.0'?>

标签: phpxmlcodeigniter-3

解决方案


无法使用任何 XML 处理库直接读取它,因为它是无效的 XML 文档。我可以想到两种方法来做到这一点。

第一个涉及操作<?xml ?>标签以使其成为普通标签并将整个文档包装在一个公共标签中(<base>在这种情况下,但这并不重要)。然后您可以正常加载整个文档并提取数据...

$data = str_replace(["<?", "?>"], ["<", "/>"], $data);
$xml = simplexml_load_string("<base>".$data."</base>");
foreach ( $xml->Properties->Property as $property ) {
    echo $property->Prop_RefId.PHP_EOL;
}
echo PHP_EOL;
foreach ( $xml->Images->Image as $image )   {
    echo $image->Prim_RefId.PHP_EOL;
}
echo PHP_EOL;

您需要记住的是,在现有结构之上还有一层,这就是我提到的原因$xml->Properties->Property

第二个是将文档拆分为各个部分。使用explode()<?xml ?>标签为分隔符,然后像往常一样处理每个部分。不利的一面是,如果声明更改,那么这将失败。这样做的好处是,如果您传递的是原始 XML 文档,它的工作方式也是一样的。

$list = explode("<?xml version='1.0'?>", $data );
$xml = simplexml_load_string ( $list[1] );
foreach ( $xml->Property as $property ) {
    echo $property->Prop_RefId.PHP_EOL;
}
echo PHP_EOL;
$xml = simplexml_load_string ( $list[2] );
foreach ( $xml->Image as $image )   {
     echo $image->Prim_RefId.PHP_EOL;
}
echo PHP_EOL;

推荐阅读