dougu5886 2019-02-19 11:02
浏览 73
已采纳

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

I get Data as below in XML Format.

Now how to Store Values of data like Description,CarMake,etc. Which is in between { } Brackets.

I want to Store Them Seperately in 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>
  • 写回答

1条回答 默认 最新

  • douqiang6036 2019-02-19 11:33
    关注

    See if this works. Create a php file and do the following code:

    <?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;
    ?>
    

    As you can see it's doing what you want. Storing all of your XML data in a php variable which is $xmlstr. Then you store that variable inside another with the same name that your using which is Vehicle. Then you use simple php to get the data that you want! Let me know if it worked!

    P.S - if you want to store each data separately in a different variable you can easily do:

    $description = $Vehicle->vehicleData->Description;
    echo "Description:" . $description . '<br>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)