Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.