簡體   English   中英

PHP的XML加載文件

[英]XML load file with PHP

我不確定自己在做什么錯,但是我無法正確地用PHP加載XML。 我嘗試使用加載文檔和simplexml_load_file等,但是我一直都在出錯。

我嘗試的示例代碼:

$xnl_file = "xml.xml";
$xml = simplexml_load_file($xnl_file);
var_dump($xml);

錯誤我得到:

Warning: simplexml_load_file(): xml.xml:3: parser error : Extra content at the end of the document in C:\xampp\htdocs\xampp\provisioning\index.php on line 3

Warning: simplexml_load_file(): <PartnershipList xmlns="http://www.audatex.com/SAXIF"> in C:\xampp\htdocs\xampp\provisioning\index.php on line 3

Warning: simplexml_load_file(): ^ in C:\xampp\htdocs\xampp\provisioning\index.php on line 3
bool(false)

XML看起來像:

<?xml version="1.0" encoding="UTF-8"?>
<CreationDateTime>Wed Nov 05 12:30:38 CET 2014</CreationDateTime>
<PartnershipList xmlns="http://www.audatex.com/SAXIF">
 <Partnership>
  <CountryOrganizationId>CZ</CountryOrganizationId>
  <PartnershipId>Contract_58AB4635-D9C6-A04E</PartnershipId>
  <OwnerId>MM-O-BDD15299</OwnerId>
  <PartnerIdList>
   <String>MM-O-2A10BCF</String>
  </PartnerIdList>
  <Contract>
   <CountryOrganizationId>CZ</CountryOrganizationId>
   <ContractId>Contract_58AB4635-D9C6-A04E</ContractId>
   <LaborRateList>
    <LaborRateDetail>
     <Role>Labor</Role>
     <Category>1</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">250.0</Rate>
    </LaborRateDetail>
    <LaborRateDetail>
     <Role>Paint</Role>
     <Category>2</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">350.0</Rate>
    </LaborRateDetail>
   </LaborRateList>
   <IdBlockCodeList>
    <IDBlockCode>
     <IDBlockCode>51</IDBlockCode>
     <Entry>100</Entry>
    </IDBlockCode>
   </IdBlockCodeList>
   <VehicleKind>Car</VehicleKind>
   <RepairKind>BodyRepair</RepairKind>
   <ManufacturerCode>07</ManufacturerCode>
   <Status>Active</Status>
   <CreatedBy>MM-P-69F997009BBFB4FC2C</CreatedBy>
   <CreationTimeStamp>2014-09-09T15:17:46.000</CreationTimeStamp>
   <UpdatedBy>MM-P-69F997009BBFB4FC2C</UpdatedBy>
   <UpdateTimeStamp>2014-10-15T10:49:18.000</UpdateTimeStamp>
   <FirstVersionContractId>Contract_58AB4635-D9C6-A04E</FirstVersionContractId>
   <OwnerId>MM-O-BDD15299</OwnerId>
   <Manufacturer>07</Manufacturer>
   <VehicleType>Car</VehicleType>
   <VehicleAgeFrom>0</VehicleAgeFrom>
   <VehicleAgeTo>0</VehicleAgeTo>
   <ClaimType>Unknown</ClaimType>
  </Contract>
  <Description>Alfa Romeo</Description>
  <PartnerId>MM-O-2A10BCF</PartnerId>
 </Partnership>
 <Partnership>
  <CountryOrganizationId>CZ</CountryOrganizationId>
  <PartnershipId>Contract_F5134A37-F39A-823A</PartnershipId>
  <OwnerId>MM-O-BDD15299</OwnerId>
  <PartnerIdList>
   <String>MM-O-2A10BCF</String>
  </PartnerIdList>
  <Contract>
   <CountryOrganizationId>CZ</CountryOrganizationId>
   <ContractId>Contract_F5134A37-F39A-823A</ContractId>
   <LaborRateList>
    <LaborRateDetail>
     <Role>Labor</Role>
     <Category>1</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">250.0</Rate>
    </LaborRateDetail>
    <LaborRateDetail>
     <Role>Paint</Role>
     <Category>2</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">350.0</Rate>
    </LaborRateDetail>
   </LaborRateList>
   <IdBlockCodeList>
    <IDBlockCode>
     <IDBlockCode>51</IDBlockCode>
     <Entry>100</Entry>
    </IDBlockCode>
   </IdBlockCodeList>
   <VehicleKind>Car</VehicleKind>
   <RepairKind>BodyRepair</RepairKind>
   <ManufacturerCode>10</ManufacturerCode>
   <Status>Active</Status>
   <CreatedBy>MM-P-69F997009BBFB4FC2C</CreatedBy>
   <CreationTimeStamp>2014-09-09T15:22:27.000</CreationTimeStamp>
   <UpdatedBy>MM-P-69F997009BBFB4FC2C</UpdatedBy>
   <UpdateTimeStamp>2014-10-15T13:11:36.000</UpdateTimeStamp>
   <FirstVersionContractId>Contract_F5134A37-F39A-823A</FirstVersionContractId>
   <OwnerId>MM-O-BDD15299</OwnerId>
   <Manufacturer>10</Manufacturer>
   <VehicleType>Car</VehicleType>
   <VehicleAgeFrom>0</VehicleAgeFrom>
   <VehicleAgeTo>0</VehicleAgeTo>
   <ClaimType>Unknown</ClaimType>
  </Contract>
  <Description>Citroën</Description>
  <PartnerId>MM-O-2A10BCF</PartnerId>
 </Partnership>

我在Google上搜索了很多教程,但看起來一開始我就做錯了。 有人可以給我任何建議嗎?

謝謝

正如TRiG所說,XML是不兼容的。 除了構建自定義解析器之外,您還可以在解析XML內容之前對其進行編輯。 XML文件應如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<PartnerContainer>
<CreationDateTime>Wed Nov 05 12:30:38 CET 2014</CreationDateTime>
<PartnershipList xmlns="http://www.audatex.com/SAXIF">
 <Partnership>
  <CountryOrganizationId>CZ</CountryOrganizationId>
  <PartnershipId>Contract_58AB4635-D9C6-A04E</PartnershipId>
  <OwnerId>MM-O-BDD15299</OwnerId>
  <PartnerIdList>
   <String>MM-O-2A10BCF</String>
  </PartnerIdList>
  <Contract>
   <CountryOrganizationId>CZ</CountryOrganizationId>
   <ContractId>Contract_58AB4635-D9C6-A04E</ContractId>
   <LaborRateList>
    <LaborRateDetail>
     <Role>Labor</Role>
     <Category>1</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">250.0</Rate>
    </LaborRateDetail>
    <LaborRateDetail>
     <Role>Paint</Role>
     <Category>2</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">350.0</Rate>
    </LaborRateDetail>
   </LaborRateList>
   <IdBlockCodeList>
    <IDBlockCode>
     <IDBlockCode>51</IDBlockCode>
     <Entry>100</Entry>
    </IDBlockCode>
   </IdBlockCodeList>
   <VehicleKind>Car</VehicleKind>
   <RepairKind>BodyRepair</RepairKind>
   <ManufacturerCode>07</ManufacturerCode>
   <Status>Active</Status>
   <CreatedBy>MM-P-69F997009BBFB4FC2C</CreatedBy>
   <CreationTimeStamp>2014-09-09T15:17:46.000</CreationTimeStamp>
   <UpdatedBy>MM-P-69F997009BBFB4FC2C</UpdatedBy>
   <UpdateTimeStamp>2014-10-15T10:49:18.000</UpdateTimeStamp>
   <FirstVersionContractId>Contract_58AB4635-D9C6-A04E</FirstVersionContractId>
   <OwnerId>MM-O-BDD15299</OwnerId>
   <Manufacturer>07</Manufacturer>
   <VehicleType>Car</VehicleType>
   <VehicleAgeFrom>0</VehicleAgeFrom>
   <VehicleAgeTo>0</VehicleAgeTo>
   <ClaimType>Unknown</ClaimType>
  </Contract>
  <Description>Alfa Romeo</Description>
  <PartnerId>MM-O-2A10BCF</PartnerId>
 </Partnership>
 <Partnership>
  <CountryOrganizationId>CZ</CountryOrganizationId>
  <PartnershipId>Contract_F5134A37-F39A-823A</PartnershipId>
  <OwnerId>MM-O-BDD15299</OwnerId>
  <PartnerIdList>
   <String>MM-O-2A10BCF</String>
  </PartnerIdList>
  <Contract>
   <CountryOrganizationId>CZ</CountryOrganizationId>
   <ContractId>Contract_F5134A37-F39A-823A</ContractId>
   <LaborRateList>
    <LaborRateDetail>
     <Role>Labor</Role>
     <Category>1</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">250.0</Rate>
    </LaborRateDetail>
    <LaborRateDetail>
     <Role>Paint</Role>
     <Category>2</Category>
     <Rate Cur="CZK" Unit="h" MinValue="0" MaxValue="0">350.0</Rate>
    </LaborRateDetail>
   </LaborRateList>
   <IdBlockCodeList>
    <IDBlockCode>
     <IDBlockCode>51</IDBlockCode>
     <Entry>100</Entry>
    </IDBlockCode>
   </IdBlockCodeList>
   <VehicleKind>Car</VehicleKind>
   <RepairKind>BodyRepair</RepairKind>
   <ManufacturerCode>10</ManufacturerCode>
   <Status>Active</Status>
   <CreatedBy>MM-P-69F997009BBFB4FC2C</CreatedBy>
   <CreationTimeStamp>2014-09-09T15:22:27.000</CreationTimeStamp>
   <UpdatedBy>MM-P-69F997009BBFB4FC2C</UpdatedBy>
   <UpdateTimeStamp>2014-10-15T13:11:36.000</UpdateTimeStamp>
   <FirstVersionContractId>Contract_F5134A37-F39A-823A</FirstVersionContractId>
   <OwnerId>MM-O-BDD15299</OwnerId>
   <Manufacturer>10</Manufacturer>
   <VehicleType>Car</VehicleType>
   <VehicleAgeFrom>0</VehicleAgeFrom>
   <VehicleAgeTo>0</VehicleAgeTo>
   <ClaimType>Unknown</ClaimType>
  </Contract>
  <Description>Citroën</Description>
  <PartnerId>MM-O-2A10BCF</PartnerId>
 </Partnership>
 </PartnershipList>
</PartnerContainer>

在PHP中,調用file_get_contents以獲取XML內容,以便您可以使用諸如str_replace功能來編輯XML內容。 之后,您可以使用simplexml_load_string解析XML字符串。 我認為如果您執行以下操作,您已經完成了,但是我不確定,沒有測試:

$xnl_file = "xml.xml";
$xml = file_get_contents($xnl_file);
$xml = str_replace('<CreationDateTime>','<PartnerContainer><CreationDateTime>',$xml);
$xml .= '</PartnershipList></PartnerContainer>';

您有一個無效的XML文檔。

<?xml version="1.0" encoding="UTF-8"?>
<CreationDateTime>Wed Nov 05 12:30:38 CET 2014</CreationDateTime>

這是一個完整的XML文檔。 它有一個開始標簽和一個結束標簽,僅此而已。 本文檔末尾的任何其他內容將使其無效。 一個XML文檔只能有一個根標簽。

如果您修復XML文檔(也許通過將<CreationDateTime>移到<CreationDateTime> <PartnershipList> ,並將<PartnershipList>設為根標記),PHP的SimpleXML庫將起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM