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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?