我想在多个较小的文件上分割1个XML文件,总是来自tag。很快PHP就是我所知道的唯一一种我决定使用它的语言。
我的尝试看起来像:
$xnl_file = "xml.xml";
$xml = simplexml_load_file($xnl_file);
$my_file = 0;
foreach ($xml as $value){
$CountryOrganizationId = "<CountryOrganizationId>".$xml->Partnership->CountryOrganizationId."</CountryOrganizationId>";
$PartnershipId = "<PartnershipId>".$xml->Partnership->PartnershipId."</PartnershipId>";
$OwnerId = "<OwnerId>".$xml->Partnership->OwnerId."<OwnerId>";
$PartnerIdList = "<PartnerIdList><String>".$xml->Partnership->PartnerIdList->String."</String></PartnerIdList>";
$CountryOrganizationId_contact = "<Contract><CountryOrganizationId>".$xml->Partnership->Contract->CountryOrganizationId."</CountryOrganizationId>";
$ContractId = "<ContractId>".$xml->Partnership->Contract->CountryOrganizationId."</ContractId>";
$Role = "<LaborRateList><LaborRateDetail><Role>".$xml->Partnership->LaborRateList->LaborRateDetail->Role."</Role>";
$Category = "<Category>".$xml->Partnership->LaborRateList->LaborRateDetail->Category."</Category>";
$Rate1 = "<Rate Cur='CZK' Unit='h' MinValue='0' MaxValue='0'>".$xml->Partnership->LaborRateList->LaborRateDetail->Rate."</Rate></LaborRateDetail>";
echo $OwnerId;
echo "<br>";
$my_file = ($my_file + 1).".xml";
//mkdir("new", 0700); Create subfolder
$handle = fopen('xml/'.$my_file, 'w') or die('Cannot open file: '.$my_file); //implicitly creates files in XML folder
$data = "<Partnership>".$CountryOrganizationId.$PartnershipId.$OwnerId.$PartnerIdList.$CountryOrganizationId_contact.$ContractId.$Role.$Category.$Rate1."</Partnership>";
fwrite($handle, $data);
}
正如您所看到的,我只是加载XML文件并预先获取值并将它们添加到变量,然后再次尝试打印。找到标签合作伙伴时,是不是有更快的方法来进行CUT?
我的XML看起来像:
<PartnershipList xmlns="URL">
<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 :(得分:1)
如果我理解正确,您想为每个<Partnership>
元素创建一个XML文件。使用DOM非常容易。
xmlns="URL"
的值不仅是一个URL - 它是文档的命名空间。它定义了元素所属的XML格式。这是一个相关信息。
DOM就是节点。 XML文档中的任何内容都是节点。这是元素节点,文本节点等等。 Xpath允许您选择节点,DOM有方法来创建,克隆和导入它们。
// create a dom and load the xml document
$source = new DOMDocument();
$source->load($xmlFile);
// get an Xpath object and register a prefix for the used namespace
$xpath = new DOMXpath($source);
$xpath->registerNamespace('ps', 'urn:missing-namespace-uri');
// fetch all namespace nodes and iterate them
foreach ($xpath->evaluate('//ps:Partnership') as $index => $partnership) {
// create a target document
$target = new DOMDocument();
// import the partnership node and append it to the target document node
$target->appendChild($target->importNode($partnership, TRUE));
// save it
echo $target->save(($index + 1).'.xml');
}
提示:将XML构建为文本需要转义(htmlspecialchars()
)。如果使用DOM,它将为您解决问题。