Jump to content

formatting xml


optikalefx

Recommended Posts

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??

Link to comment
https://forums.phpfreaks.com/topic/82461-formatting-xml/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-419261
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-419497
Share on other sites

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-420070
Share on other sites

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>


Link to comment
https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-420208
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/82461-formatting-xml/#findComment-421013
Share on other sites

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.