PHP加载XML文件

时间:2014-11-05 12:11:49

标签: php xml

我不确定我做错了什么,但我无法正确加载我的PHP。 我尝试使用load document和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>

我谷歌很多教程,但看起来我在开始时做错了什么。有人可以给我任何建议吗?

由于

2 个答案:

答案 0 :(得分:2)

正如TRiG已经说过的那样,XML是无用的。您可以在解析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>';

答案 1 :(得分:0)

您的XML文档无效。

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

这是一个完整的XML文档。它有一个开始标记和一个结束标记就是这样。附加到本文档末尾的任何其他内容都将使其无效。 XML文档只能有一个根标记。

如果您修复XML文档(可能通过在<CreationDateTime>内移动<PartnershipList>,并使<PartnershipList>成为您的根标记),PHP的SimpleXML库将起作用。