Split 1 XML to multiple after specific tag using PHP -
Split 1 XML to multiple after specific tag using PHP -
i want split 1 xml file on multiple smaller one, tag . php language know bit decide utilize it.
my seek looks like:
$xnl_file = "xml.xml"; $xml = simplexml_load_file($xnl_file); $my_file = 0; foreach ($xml $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 can see load xml file , foreach getting values , adding them variabiles , trying print again. isn't there faster way cutting when find 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 advise
if understand correctly want create xml file each <partnership>
element. quite easy dom.
the value of xmlns="url"
not url - namespace of document. defines xml format elements part of. relevant information.
dom nodes. in xml document node. here element nodes, text nodes , many more. xpath allows select nodes , dom has methods create, clone , import them.
// create dom , load xml document $source = new domdocument(); $source->load($xmlfile); // xpath object , register prefix used namespace $xpath = new domxpath($source); $xpath->registernamespace('ps', 'urn:missing-namespace-uri'); // fetch namespace nodes , iterate them foreach ($xpath->evaluate('//ps:partnership') $index => $partnership) { // create target document $target = new domdocument(); // import partnership node , append target document node $target->appendchild($target->importnode($partnership, true)); // save echo $target->save(($index + 1).'.xml'); }
hint: building xml text requires escaping (htmlspecialchars()
). if using dom, take care of escaping you.
php xml
Comments
Post a Comment