Jump to content

[SOLVED] How to make linbreak in printed XML, between "createTextNode()"??


physaux

Recommended Posts

So i am trying to write an xml file. I am trying to write a part like this:

 

<configuration>
variable1 = "2"
variable2 = "9"
</configuration>

 

But my code gives me it all one line, without a linebreak:

 

<configuration>variable1 = "2"variable2 = "9"</configuration>

 

How can I make a linebreak? I have tried \n, it just prints out "\n". I have tried <br>, it just prints out "<br>" (I put them both with the var1 string, in the same brackets)

Any suggestions? Here is my relevant code:

 

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

$l1 = $doc->createElement( "main" );
$doc->appendChild( $l1 );

$l2 = $doc->createElement( "configuration" );
$doc->appendChild( $l2 );

$l2->appendChild($doc->createTextNode('variable1 = "2"' ));
$l2->appendChild($doc->createTextNode('variable2 = "9"' ));

$l1->appendChild( $l2 );
$doc->saveXML();
$doc->save("write.xml") ;

Not sure if it will work, but worth a shot:

 

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

$l1 = $doc->createElement( "main" );
$doc->appendChild( $l1 );

$l2 = $doc->createElement( "configuration" );
$doc->appendChild( $l2 );

$l2->appendChild($doc->createTextNode("\nvariable1 = \"2\"\n" ));
$l2->appendChild($doc->createTextNode("\nvariable2 = \"9\"\n" ));

$l1->appendChild( $l2 );
$doc->saveXML();
$doc->save("write.xml") ;

 

\n only is interepted inside of double quotes, single quotes takes it literally, give that a shot.

Ah, that is awesome It worked!

 

 

 

 

buuuut...

 

That led me to realize that I actually outputted the wrong thing, I now output(like I wanted):

<configuration>
variable1 = "2"
variable2 = "9"
</configuration>

 

:wtf:

but I was getting errors, and I finally realized I need to actually have it like this:

<configuration
variable1 = "2"
variable2 = "9"
>
</configuration>

 

So the new line is working, but now I need to put the "variables" inside of the "<>".

I tried just printing the raw text, but it won't let me. It keeps html encoding the "<>", so I cannot use print to get this done.

 

-I'm starting to think new lines was the wrong idea, i should have been doing attributes or something.. :confused: :confused:

 

Any help? thankyou!

 

 

 

EDIT: Ok nevermind, the proper solution was simple and well documented, so i got it now. Thanks

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.