ctcudd Posted July 9, 2009 Share Posted July 9, 2009 I'm stumped... I am trying to write the contents of an array to an xml file. I want the xml file to look similar to this <chart> <chart_data> <row> <null/> <string>TCC</string> <string>TED</string> <string>HYJ</string> <string>HBA</string> <string>NDM</string> </row> <row> <string>work_completes</string> <number>4</number> <number>3</number> <number>1</number> <number>1</number> <number>1</number> </row> </chart_data> </chart> I am using the following code: $doc = new DOMDocument(); $doc->formatOutput = true; $chart = $doc->createElement( "chart" ); $doc->appendChild( $chart ); $chart_data = $doc->createElement( "chart_data" ); $chart->appendChild( $chart_data); $row = $doc->createElement( "row" ); $chart_data->appendChild( $row); $string = $doc->createElement( "null" ); $row->appendChild( $string); foreach( $tests as $test ) { $string = $doc->createElement( "string" ); $author = $doc->createElement( "string" ); $author->appendChild( $doc->createTextNode( $test['login'] ) ); $row->appendChild( $author ); $title = $doc->createElement( "string" ); $title->appendChild( $doc->createTextNode( $test['login'] ) ); $row->appendChild( $title ); $publisher = $doc->createElement( "string" ); $publisher->appendChild( $doc->createTextNode( $test['wc'] ) ); $row->appendChild( $publisher ); } $row = $doc->createElement( "row" ); $chart_data->appendChild( $row); $string = $doc->createElement( "null" ); $row->appendChild( $string); echo $doc->saveXML(); $doc->save("test.xml"); and I am getting this in my xml file: <chart> <chart_data> <row> <null/> </row> <row> <null/> </row> </chart_data> </chart> The contents of the arrary that I am using ($test) is: { ["TCC"]=> int(4) ["TED"]=> int(3) ["HYJ"]=> int(2) ["HBA"]=> int(1) ["NDM"]=> int(1) } It seems like my script is failing to create an element but only inside the foreach loops. Greatly appreciate any help I can get. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/165400-cannot-add-text-node-within-foreach-loop/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.