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
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
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
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
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
Share on other sites

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.