Example

Example

Example -- Example for the usage of XML_Beautifier

Basic example

Let's assume, you've got an XML document that is really badly formatted:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE page [
    <!ENTITY foo SYSTEM "foo.xml">
    <!ENTITY bar SYSTEM "bar.xml">
]>
<document title="Current News">
<meta project="none" foo="bar">
    <keywords/><description/>
<author>Stephan Schmidt</author>
<getMetaNav/>
</meta>
<page label="PHP Application Tools" sublabel="Current News">
<?php
    for($i = 0; $i < count($_GET); $i++) {
        echo    $_GET[$i]."<br>";
    }
?>
&foo;&bar;
<intro>
   <!-- This Comment
has more
    than one line.
                    -->
        <introtitle>Welcome to PHP
Application Tools &amp; PEAR!</introtitle>
<para>
        If you're new to pat, and would like
        <!-- This is a comment in a single line that contains an &amp; -->
         to know
         
         what we do
         
         here, take a look at
         
         <link url=
         "/about/project.xml">
         "About Pat"</link> 
or
check out the
<link url="/about/projectsOverview.xml">"projects overview"</link>. Otherwise, you probably know your way 
around
the site already <smiley type="smile"/>
</para>
                                            </intro>
</page>
</document>

All you have to do is to use XML_Beautifier, pass the filename of the original file and the filename for the new file:

require_once "XML/Beautifier.php";
$fmt = new XML_Beautifier();
$result = $fmt->formatFile('originalFile.xml', 'beautifiedFile.xml');

if (PEAR::isError($result)) {
    echo $result->getMessage();
    exit();
}
echo "File beautified, awaiting orders!<br />";

And voila, your document looks nice:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE page [
    <!ENTITY foo SYSTEM "foo.xml">
    <!ENTITY bar SYSTEM "bar.xml">
]>
<document title="Current News">
    <meta foo="bar" project="none">
        <keywords />
        <description />
        <author>Stephan Schmidt</author>
        <getMetaNav />
    </meta>
    <page label="PHP Application Tools" sublabel="Current News">
        <?php
        for($i = 0; $i < count($_GET); $i++) {
                echo    $_GET[$i]."<br>";
            }
        ?>
        &foo;
        &bar;
        <intro>
            <!--
                This Comment
                has more
                than one line.
            -->
            <introtitle>Welcome to PHP Application Tools &amp; PEAR!</introtitle>
            <para>
                If you&apos;re new to pat, and would like
                <!-- This is a comment in a single line that contains an &amp; -->
                to know what we do here, take a look at
                <link url="/about/project.xml">&quot;About Pat&quot;</link>
                or check out the
                <link url="/about/projectsOverview.xml">&quot;projects overview&quot;</link>
                . Otherwise, you probably know your way around the site already
                <smiley type="smile" />
            </para>
        </intro>
    </page>
</document>
© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.