在 PHP 中提取 XML 中包含的一些 JSON
Posted
技术标签:
【中文标题】在 PHP 中提取 XML 中包含的一些 JSON【英文标题】:Extract some JSON contained in an XML in PHP 【发布时间】:2021-01-16 20:51:45 【问题描述】:我正在尝试提取我的 XML 文件中包含的一些 JSON,然后检索这些值。
我的 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": "RENAULT SCÉNIC III",
"RegistrationYear": "2016",
"CarMake":
"CurrentTextValue": "RENAULT"
,
"CarModel":
"CurrentTextValue": "SCÉNIC III"
,
"EngineSize":
"CurrentTextValue": "5"
,
"FuelType":
"CurrentTextValue": "DIESEL"
,
"MakeDescription":
"CurrentTextValue": "RENAULT"
,
"ModelDescription":
"CurrentTextValue": "SCÉNIC III"
,
"Immobiliser":
"CurrentTextValue": ""
,
"IndicativeValue":
"CurrentTextValue": 0
,
"DriverSide":
"CurrentTextValue": ""
,
"BodyStyle":
"CurrentTextValue": "MONOSPACE COMPACT"
,
"RegistrationDate": "2016-06-24",
"ImageUrl": "http://immatriculationapi.com/image.aspx/@UkVOQVVMVCBTQ8OJTklDIElJSQ==",
"ExtendedData":
"anneeSortie": "2016",
"boiteDeVitesse": "",
"carburantVersion": "D",
"carrosserieVersion": "",
"classeSra": "",
"libVersion": "1.5 dCi 1461cm3 110cv ",
"libelleModele": "SCÉNIC III",
"marque": "RE",
"modele": "",
"produit": "",
"puissance": "5",
"version": "",
"cleCarrosserie": "",
"groupeSra": "",
"nbPlace": "5",
"datePremiereMiseCirculation": "24062016",
"questionBatterie": "",
"electrique": "",
"genre": "",
"typeVehicule": "",
"numSerieMoteur": "VF1JZ890H55864144",
"valeurANeufSRA": "",
"niveauRisqueVol": "",
"protectionConstructeur": "",
"puissanceDyn": "110",
"segmentVeh": "",
"KtypeId": "5853",
"EngineCC": "1461",
"Co2": "105",
"Cylinders": "4",
"CNIT": "M10RENVP472E768"
</vehicleJson>
<vehicleData>
<Description>RENAULT SCÉNIC III</Description>
<RegistrationYear>2016</RegistrationYear>
<CarMake>
<CurrentTextValue>RENAULT</CurrentTextValue>
</CarMake>
<CarModel>SCÉNIC III</CarModel>
<BodyStyle>
<CurrentTextValue>MONOSPACE COMPACT</CurrentTextValue>
</BodyStyle>
<EngineSize>
<CurrentTextValue>5</CurrentTextValue>
</EngineSize>
<NumberOfDoors>
<CurrentValue>5</CurrentValue>
</NumberOfDoors>
<Transmission>
<CurrentValue />
</Transmission>
<FuelType>
<CurrentTextValue>DIESEL</CurrentTextValue>
</FuelType>
<Immobiliser>
<CurrentTextValue />
</Immobiliser>
<NumberOfSeats>
<CurrentValue>5</CurrentValue>
</NumberOfSeats>
</vehicleData>
</Vehicle>
第 1 步:我正在寻找包含在 ExtendedData 中的 Description 和一些值:
$xml = simplexml_load_string($my_xml) or die("Error: Cannot create object");
$infos_plaque = json_encode($xml->vehicleJson); // The attribute where the JSON is stored
$array = json_decode($infos_plaque);
echo "<pre>";
print_r($array);
echo "</pre>";
这个 $array 返回:
stdClass Object
(
[0] =>
"Description": "RENAULT SCÉNIC III",
"RegistrationYear": "2016",
"CarMake":
"CurrentTextValue": "RENAULT"
,
"CarModel":
"CurrentTextValue": "SCÉNIC III"
,
"EngineSize":
"CurrentTextValue": "5"
,
"FuelType":
"CurrentTextValue": "DIESEL"
,
"MakeDescription":
"CurrentTextValue": "RENAULT"
,
"ModelDescription":
"CurrentTextValue": "SCÉNIC III"
,
"Immobiliser":
"CurrentTextValue": ""
,
"IndicativeValue":
"CurrentTextValue": 0
,
"DriverSide":
"CurrentTextValue": ""
,
"BodyStyle":
"CurrentTextValue": "MONOSPACE COMPACT"
,
"RegistrationDate": "2016-06-24",
"ImageUrl": "http://immatriculationapi.com/image.aspx/@UkVOQVVMVCBTQ8OJTklDIElJSQ==",
"ExtendedData":
"anneeSortie": "2016",
"boiteDeVitesse": "",
"carburantVersion": "D",
"carrosserieVersion": "",
"classeSra": "",
"libVersion": "1.5 dCi 1461cm3 110cv ",
"libelleModele": "SCÉNIC III",
"marque": "RE",
"modele": "",
"produit": "",
"puissance": "5",
"version": "",
"cleCarrosserie": "",
"groupeSra": "",
"nbPlace": "5",
"datePremiereMiseCirculation": "24062016",
"questionBatterie": "",
"electrique": "",
"genre": "",
"typeVehicule": "",
"numSerieMoteur": "VF1JZ890H55864144",
"valeurANeufSRA": "",
"niveauRisqueVol": "",
"protectionConstructeur": "",
"puissanceDyn": "110",
"segmentVeh": "",
"KtypeId": "5853",
"EngineCC": "1461",
"Co2": "105",
"Cylinders": "4",
"CNIT": "M10RENVP472E768"
)
第2步:我尝试通过提出不同的请求来检索值:
print_r($array->Description);
print_r($array->'RegistrationDate');
print_r($array->'ExtendedData'->'puissance');
print_r($array->ExtendedData->puissance);
// or :
echo $array->Description;
echo $array->ExtendedData->puissance;
echo $array->ExtendedData->libVersion;
我认为在编码 Json 时存在问题,我尝试了不同的解决方案但没有成功。
【问题讨论】:
【参考方案1】:您只需要将 JSON解码 成 php 数据结构。然后你就可以访问它的属性了
$xml = simplexml_load_string($my_xml);
$data = json_decode($xml->vehicleJson); // ? just decode the string
echo $data->Description;
echo $data->ExtendedData->puissance;
echo $data->ExtendedData->libVersion;
演示~https://3v4l.org/ZjE9H
【讨论】:
以上是关于在 PHP 中提取 XML 中包含的一些 JSON的主要内容,如果未能解决你的问题,请参考以下文章
VBA/VBScript提取Word(*.doc)文件中包含的图片(照片)