如何将数据从xml存储到php变量,这是单个标记内的数据集合,如数组

问题描述 投票:0回答:1

我在XML格式中获得如下数据。

现在如何存储描述,CarMake等数据的值。这是在{}括号之间。

我想在php中分别存储它们。

<Vehicle xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://regcheck.org.uk">
<vehicleJson>
{ "Description": "JCB INDIA LIMITED / JCB 3DX", "RegistrationYear": "2010", "CarMake": { "CurrentTextValue": "JCB INDIA LIMITED" }, "CarModel": { "CurrentTextValue": "JCB 3DX" }, "MakeDescription": { "CurrentTextValue": "JCB INDIA LIMITED" }, "ModelDescription": { "CurrentTextValue": "JCB 3DX" }, "VechileIdentificationNumber": "1710000", "EngineNumber": "XXXXX", "FuelType": { "CurrentTextValue": "DIESEL" }, "RegistrationDate": "13/08/2010", "Owner": "RAMDEBHAI KARANGIYA", "Fitness": "18-Sep-2019", "Insurance": "05-Nov-2019", "Location": "RTO,PORBANDAR HIGHWAY", "ImageUrl": "http://in.carregistrationapi.com/image.aspx/@SkNCIElORElBIExJTUlURUQgLyBKQ0IgM0RY" }
</vehicleJson>
<vehicleData>
<Description>JCB INDIA LIMITED / JCB 3DX</Description>
<RegistrationYear>2010</RegistrationYear>
<CarMake>
<CurrentTextValue>JCB INDIA LIMITED</CurrentTextValue>
</CarMake>
<CarModel>JCB 3DX</CarModel>
</vehicleData>
</Vehicle>
javascript php html xml xml-parsing
1个回答
0
投票

看看这是否有效。创建一个php文件并执行以下代码:

<?php

$xmlstr = <<<XML
<Vehicle xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://regcheck.org.uk">
<vehicleJson>
{ "Description": "JCB INDIA LIMITED / JCB 3DX", "RegistrationYear": "2010", "CarMake": { "CurrentTextValue": "JCB INDIA LIMITED" }, "CarModel": { "CurrentTextValue": "JCB 3DX" }, "MakeDescription": { "CurrentTextValue": "JCB INDIA LIMITED" }, "ModelDescription": { "CurrentTextValue": "JCB 3DX" }, "VechileIdentificationNumber": "1710000", "EngineNumber": "XXXXX", "FuelType": { "CurrentTextValue": "DIESEL" }, "RegistrationDate": "13/08/2010", "Owner": "RAMDEBHAI KARANGIYA", "Fitness": "18-Sep-2019", "Insurance": "05-Nov-2019", "Location": "RTO,PORBANDAR HIGHWAY", "ImageUrl": "http://in.carregistrationapi.com/image.aspx/@SkNCIElORElBIExJTUlURUQgLyBKQ0IgM0RY" }
</vehicleJson>
<vehicleData>
<Description>JCB INDIA LIMITED / JCB 3DX</Description>
<RegistrationYear>2010</RegistrationYear>
<CarMake>
<CurrentTextValue>JCB INDIA LIMITED</CurrentTextValue>
</CarMake>
<CarModel>JCB 3DX</CarModel>
</vehicleData>
</Vehicle>
XML;

?>

<?php 
$Vehicle = new SimpleXMLElement($xmlstr);

echo "Description:" . $Vehicle->vehicleData->Description . '<br>';
echo "Registration Year: " . $Vehicle->vehicleData->RegistrationYear;
?>

你可以看到它正在做你想要的。将所有XML数据存储在php变量$xmlstr中。然后将该变量存储在另一个变量中,其名称与使用Vehicle的名称相同。然后你使用简单的PHP来获取你想要的数据!让我知道它是否有效!

P.S - 如果您想将每个数据分别存储在不同的变量中,您可以轻松地执行以下操作:

$description = $Vehicle->vehicleData->Description;
echo "Description:" . $description . '<br>';
© www.soinside.com 2019 - 2024. All rights reserved.