Jump to content

Reordering Nodes In Xml File With Php


reakt

Recommended Posts

Hi all, I have an xml file and need to insert a whole bunch of nodes into it as children of a particular node. Here is the file I start with:

<Header Organisation_Name="XXXXXXX" Run_Date="20090127" Run_UserId="PJ421" Version="18">	<NoticeRun Id="81" Run_Name="Journals 7928,7957,8058,8067" Issue_Date="20090129" No_of_Notices="122">		<InstalmentPlan Name="Supp Levies Jan09" Discount_Date_1="20090302" Discount_Percent_1="10.00">			<Instalment Number="1" Due_Date="20090302" />		</InstalmentPlan>		<Group Name="{Ungrouped}" />	</NoticeRun></Header>

I have a whole bunch of xml files named Notice_ID.xml and need to insert them as children of the Group Node. Here is an excerpt from one of the Notice_ID.xml files:

<Notice Id="199187" Sequence="1" Amount="2495.69" Discount_Amount_1="0.00" Discounted_Amount_1="2495.69">	<FormattedNameAddr Line1="XXXXXXXXXXXXXXXX" Line2="XXXXXXXXXXXXXXXXX" Line3="XXXXXXXXXXXXXXXX" Line4="XXXXXXXXXXXXX"/>	<Assessments>		<Assessment Id="XXXXX" Is_Duplicate_Notice="0">			<Valuation Type_5="UCV07" Valuation_5="26728" Date_of_Valuation="20061001" Issue_Date="20080311"/>			<Balances>				<Balance Levy_Name="General Rate 19 Comm Title Inv" Levy_Code="04R13" Display_Seq="1" Arrears_Balance="786.81"/>			</Balances>			<Levies>				<Levy Charge_Date="20081219" Levy_Name="Legal Costs" Base_Amount="524.27"/>			</Levies>		</Assessment>	</Assessments></Notice>

And finally, here is an excerpt the code I use to grab each Notice_ID file in order, insert some new attributes, and finally output into one big file.

$doc = new DOMDocument('1.0'); $doc->formatOutput = true;$doc->load('test.xml');$sql = "SELECT BSPKey, Barcode, Notice_ID, Postcode FROM MailMerge ORDER BY BSPKey, Postcode";$result = odbc_exec($dbcnx, $sql);while ($row = odbc_fetch_array($result)) {	$BSPKey = $row['BSPKey'];	$Barcode = $row['Barcode'];	$Notice_ID = $row['Notice_ID'];	$xml = new SimpleXMLElement("notices\\$Notice_ID".".xml", null, true);	$xml->FormattedNameAddr->addAttribute("BSPKey", "$BSPKey");	$xml->FormattedNameAddr->addAttribute("Barcode", "$Barcode");	$domnode = dom_import_simplexml($xml);	$domnode = $doc->importNode($domnode, true);//	$doc->firstChild->appendChild($domnode); //This inserts the nodes as children of <Header>//	$doc->appendChild($domnode); //This inserts the nodes after </Header>	$doc->Header->NoticeRun->Group->appendChild($domnode); //This returns error "Fatal error: Call to a member function appendChild() on a non-object"}echo $doc->saveXML();

As commented in the code, the problem i'm having is inserting the <Notice> nodes in the right place. I want them to be children of the <Group> node, but come up with the error "Fatal error: Call to a member function appendChild() on a non-object". I can use either of the lines above to insert them as either children of Header, or after Header, but that's not what I need. What am I missing? Thanks

Link to comment
Share on other sites

It looks like you're trying to use simpleXML syntax on a DOMDocument, and I don't think that works. You need to use those clunky old DOM methods, like getElementsByTagName. This might work:$doc->getElementsByTagName('Group')->item(0)->appendChild($domnode);If you don't need the extra features of a DOMDocument, maybe it could all be in simpleXML?

Link to comment
Share on other sites

It looks like you're trying to use simpleXML syntax on a DOMDocument, and I don't think that works. You need to use those clunky old DOM methods, like getElementsByTagName. This might work:$doc->getElementsByTagName('Group')->item(0)->appendChild($domnode);If you don't need the extra features of a DOMDocument, maybe it could all be in simpleXML?
Nice one, that worked perfectly!The reason I convert the simplexml into dom is because the xml file i start with has no line breaks and is just one long string. I read somewhere that feeding it to dom and using formatOutput = true was an easy way to format it into nicely indented separate lines. It seems to work well so I'll stick with it for now.Thanks for the help!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...