Dianna
Dianna

Reputation: 411

Writing xml with php

I don't know where my error comes from, i am new to xml and i need some help. I want to write an xml file using php. I have the following php code:

<?php 
 $xml = array(); 
 $xml [] = array( 
 'error' => 'ERROR_ID'
  ); 

 $doc = new DOMDocument(); 
 $doc->formatOutput = true; 

 $r = $doc->createElement( "xml" ); 
 $doc->appendChild( $r ); 

 $parameters = $doc->createElement( "parameters" );    
  $error = $doc->createElement( "error" ); 
 $error->appendChild( 
 $doc->createTextNode( $xml['error'] ) 
 ); 
 $parameters->appendChild( $error ); 


 $r->appendChild( $parameters ); 


 echo $doc->saveXML(); 
 $doc->save("write.xml") 
 ?>

My xml file should look like this:

<?xml version="1.0"?>
<xml>
  <parameters>
    <error>ERROR_ID</error>
  </parameters>
</xml>

but the ERROR_ID text node doesn't show up in the file.What went wrong?

Upvotes: 0

Views: 191

Answers (1)

Jeroen
Jeroen

Reputation: 13257

First option: replace this line:

$doc->createTextNode( $xml['error'] ) 

With this one:

$doc->createTextNode( $xml [0] ['error'] ) 

Second option: replace this line:

$xml [] = array( 

With this one: (and remove the first line)

$xml = array( 

Upvotes: 4

Related Questions