2014-11-05 2 views
0

내가 뭘 잘못하고 있는지 모르겠지만 XML로 정확하게 PHP를 올바르게로드 할 수 없습니다. 로드 문서 및 simplexml_load_file 등으로 시도하지만 항상 오류가 발생합니다.PHP로 XML로드 파일

예제 코드는 내가 시도 :

$xnl_file = "xml.xml"; 
$xml = simplexml_load_file($xnl_file); 
var_dump($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> 

하지만 난 뭔가 잘못하고있는 중이 야 같습니다

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처럼 보이는 처음부터. 누군가 내게 그것에 대해 조언을 해줄 수 있습니까?

감사합니다.

+0

XML 문서가 잘못되었다는 오류 메시지가 표시됩니다. 따라서 XML 문서를 수정하십시오. – TRiG

+0

당신은 적절한 xml, 그 부모 노드가 누락 및 PartnershipList 닫는 – Ghost

답변

0

잘못된 XML 문서가 있습니다.

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

완전한 XML 문서입니다. 그것에는 시작 태그와 종료 태그가 있으며 그게 전부입니다. 이 문서의 끝에 추가 된 내용은이 문서를 무효로 만듭니다. XML 문서에는 하나의 루트 태그 만있을 수 있습니다.

<CreationDateTime><PartnershipList>으로 이동하고 <PartnershipList>을 루트 태그로 지정하여 XML 문서를 수정 한 경우 PHP의 SimpleXML 라이브러리가 작동합니다.

+0

답변을 주셔서 감사합니다, 그것은 그것이 PHP에 대한 잘못된 문서를 이해하지만 어떻게 해결할 수 있습니까? 이것은 수출입니다. – Andurit

+0

그게 당신이 가진 것이면, 당신이 가진 것은 XML이 아닙니다. XML 자체를 호출 할 수도 있지만 거짓말입니다. 해당 내보내기를 수정할 방법이 없으면 사용자 지정 파서를 직접 작성해야 할 수 있습니다. – TRiG

+0

고마워, 이제 알았어. 가능하면 곧바로 "올바른 대답"을 줄 것입니다. – Andurit

2

TRiG가 이미 말했듯이 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에서, 당신은 str_replace 같은 기능을 가진 XML 내용을 편집 할 수 있도록 XML 컨텐츠를 얻기 위해 file_get_contents 전화 : 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>';