optikalefx Posted December 20, 2007 Share Posted December 20, 2007 i asked this once before, but i wasnt clear. you know how an xml document is like <tag> <tag> <tag> <tag> well with simpleXML (google it) you can add nodes and such by way of using this: $xml = simplexml_load_file('file.xml'); $input1 = $xml->addChild('tag'); $input2 = $input1->addChild(tag inside tag'); $input2->addChild('tag',$data); really easy, i use it a lot. However, the problem is that the xml this creates is like this: <person><detail><fname>bob</fname><lname>hadababy</lname><address>itsaboy</address><skills>yahoo</skills></detail></person><person><detail><fname\n>another</fname\n><lname>test</lname><address>to</address><skills>see</skills></detail></person></sean> it creates one string, instead of line breaking. How the hell can i line break? The only way around this i guess is not use XML at all! Just have php open the file, and use javascript to getElementsByTagName and add childNode that way, with that approach i can add a \n in there. but that is voiding the point of using XML commands. any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/ Share on other sites More sharing options...
~n[EO]n~ Posted December 20, 2007 Share Posted December 20, 2007 Did you try the tutorials found here , maybe some help Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-419258 Share on other sites More sharing options...
desithugg Posted December 20, 2007 Share Posted December 20, 2007 Umm I'm not totally sure what you mean but why don't you just use preg/str replace to add \n to the string so. <?php $bbcode = array("'\</(.*?)\>'is" => "</\\1>\n"); $xml = preg_replace(array_keys($bbcode), array_values($bbcode), $xml); ?> something like that could work Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-419261 Share on other sites More sharing options...
sKunKbad Posted December 20, 2007 Share Posted December 20, 2007 n~ link=topic=172873.msg766954#msg766954 date=1198132901] Did you try the tutorials found here , maybe some help I tried the tutorial, and it still writes the tags all to one line. Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-419267 Share on other sites More sharing options...
optikalefx Posted December 20, 2007 Author Share Posted December 20, 2007 Umm I'm not totally sure what you mean but why don't you just use preg/str replace to add \n to the string so. <?php $bbcode = array("'\</(.*?)\>'is" => "</\\1>\n"); $xml = preg_replace(array_keys($bbcode), array_values($bbcode), $xml); ?> something like that could work Do you mind explaining what \</(.$?)\>'is => "</\\1>\n"); is doing? i see the escape </ and something then escape > and then after is </ something 1 >\n does (.$?) get all tags or something? and the \\1 idk Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-419497 Share on other sites More sharing options...
effigy Posted December 20, 2007 Share Posted December 20, 2007 tidy. Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-419504 Share on other sites More sharing options...
sKunKbad Posted December 21, 2007 Share Posted December 21, 2007 Tidy is really nice, but it needs to be enabled on your php installation. The following code would work perfectly for you in php5: <?php $xml = "<sean><person><detail><fname>bob</fname><lname>hadababy</lname><address>itsaboy</address><skills>yahoo</skills></detail></person><person><detail><fname\n>another</fname\n><lname>test</lname><address>to</address><skills>see</skills></detail></person></sean>"; $config = array( 'input-xml' => true, 'indent' => true, 'output-xml' => true ); $tidy = new tidy; $tidy->parseString($xml, $config, 'utf8'); $tidy->cleanRepair(); echo $tidy; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-420070 Share on other sites More sharing options...
optikalefx Posted December 21, 2007 Author Share Posted December 21, 2007 ok ive been reading the manual for like a day now and i cant find a way to install it. I know where to edit my php.ini file on my server. Im running osx leopard. Would anyone be able to lend a hand, the manual isnt helping me much at all Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-420148 Share on other sites More sharing options...
sKunKbad Posted December 21, 2007 Share Posted December 21, 2007 Yeah, I know nada about macs, but I put together a full script example that works. writeXML.php5 <?php include 'example.php'; $xml = new SimpleXMLElement($xmlstr); $character = $xml->book[0]->characters->addChild('character'); $character->addChild('name', 'Yellow Cat'); $character->addChild('desc', 'aloof'); $success = $xml->book[0]->addChild('success', '2'); $success->addAttribute('type', 'reprints'); $xml = $xml->asXML(); $config = array( 'input-xml'=> true, 'indent'=> true, 'output-xml'=> true ); $tidy = new tidy; $tidy->parseString($xml, $config, 'utf8'); $tidy->cleanRepair(); echo "<pre>"; echo htmlentities($tidy); echo "</pre>"; ?> example.php <?php $xmlstr = <<<XML <books> <book> <title>Great American Novel</title> <characters> <character> <name>Cliff</name> <desc>really great guy</desc> </character> <character> <name>Lovely Woman</name> <desc>matchless beauty</desc> </character> <character> <name>Loyal Dog</name> <desc>sleepy</desc> </character> </characters> <plot> Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark at mailman. </plot> <success type="bestseller">4</success> <success type="bookclubs">9</success> </book> </books> XML; ?> output = <?xml version="1.0"?> <books> <book> <title>Great American Novel</title> <characters> <character> <name>Cliff</name> <desc>really great guy</desc> </character> <character> <name>Lovely Woman</name> <desc>matchless beauty</desc> </character> <character> <name>Loyal Dog</name> <desc>sleepy</desc> </character> <character> <name>Yellow Cat</name> <desc>aloof</desc> </character> </characters> <plot>Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark at mailman.</plot> <success type="bestseller">4</success> <success type="bookclubs">9</success> <success type="reprints">2</success> </book> </books> Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-420208 Share on other sites More sharing options...
optikalefx Posted December 21, 2007 Author Share Posted December 21, 2007 none of it works without tidy installed, i have to keep looking into that, thanks for the example tho, ill let you if it works when get tidy Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-420654 Share on other sites More sharing options...
optikalefx Posted December 21, 2007 Author Share Posted December 21, 2007 i got it intalled yay. your script fails Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /hermes/web04/b1609/pow.4ten/htdocs/tidyphp.php on line 2 Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-420679 Share on other sites More sharing options...
sKunKbad Posted December 21, 2007 Share Posted December 21, 2007 if the error is in my example.php script (the initial XML), make sure that when you copied and pasted the XML that you didn't gain any whitespace after any of the lines, especially the heredoc tags ( <<<XML ) and ( XML; ). Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-420794 Share on other sites More sharing options...
optikalefx Posted December 22, 2007 Author Share Posted December 22, 2007 i think i installed it wrong i went to my php.ini file for php 5 and i uncommented the tidy line it was ;something blah tidy blah something and i took out the semi colon, so i actually got errors on every php script after that point... so i commented it back. idk Quote Link to comment https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-421013 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.