2014-11-05 102 views
0

我不知道我做错了什么,但我只是无法正确加载XML与我的PHP。 我尝试使用加载文档和simplexml_load_file等,但我总是收到错误。XML加载文件与PHP

示例代码我尝试:

$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> 

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

谢谢

+0

错误消息是说你的XML文档是无效的。所以修复你的XML文档。 – TRiG 2014-11-05 12:13:32

+0

你需要得到正确的XML,它缺少一个父节点,并在PartnershipList – Ghost 2014-11-05 12:17:14

回答

0

您有一个无效的XML文档。

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

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

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

+0

关闭感谢回答,我明白它看起来这是PHP的错误文档,但我该如何解决它?这是我得到的出口。 – Andurit 2014-11-05 12:17:00

+0

如果这就是你所拥有的,那么你得到的不是XML。它可能会自称XML,但这是一个谎言。如果无法解决导出问题,则可能需要构建自定义解析器。 – TRiG 2014-11-05 12:18:23

+0

谢谢,现在我明白了。我会尽快给你“正确答案” – Andurit 2014-11-05 12:18:46

2

正如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内容,以便您可以编辑与功能的XML内容,如str_replace。之后,您可以使用simplexml_load_string解析XML字符串。我认为你已经完成了如果你做了以下的事情,但我不确定,没有测试:

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