2014-11-05 130 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>';