Reputation: 5762
i want to split 1 XML file on multiple smaller one, always from tag . As soon PHP is the only language i know a bit i decide to use it.
My try looks like:
$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);
}
As you can see i just load XML file and foreach getting values and adding them to variabiles and then trying to print again. isn't there some faster way just to CUT when find a tag Partnership?
My XML looks like:
<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>
Thanks for any advise
Upvotes: 0
Views: 176
Reputation: 19492
If I understand correctly you want to create a XML file for each <Partnership>
element. This is quite easy with DOM.
The value of xmlns="URL"
is not only an URL - it is the namespace of the document. It defines the XML format the elements are part of. This is a relevant information.
DOM is all about nodes. Anything in an XML document is a node. Here are element nodes, text nodes and many more. Xpath allows you to select nodes and DOM has methods to create, clone and import them.
// 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');
}
Hint: Building XML as text requires escaping (htmlspecialchars()
). If using DOM, it will take care of the escaping for you.
Upvotes: 1