physaux Posted November 17, 2009 Share Posted November 17, 2009 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") ; Quote Link to comment https://forums.phpfreaks.com/topic/181927-solved-how-to-make-linbreak-in-printed-xml-between-createtextnode/ Share on other sites More sharing options...
premiso Posted November 17, 2009 Share Posted November 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181927-solved-how-to-make-linbreak-in-printed-xml-between-createtextnode/#findComment-959576 Share on other sites More sharing options...
physaux Posted November 17, 2009 Author Share Posted November 17, 2009 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> 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: Any help? thankyou! EDIT: Ok nevermind, the proper solution was simple and well documented, so i got it now. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/181927-solved-how-to-make-linbreak-in-printed-xml-between-createtextnode/#findComment-959634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.