例 -- XML_Beautifierの使用例

基本例

実際にひどい書式のXML文書を持っていると仮定します。

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

行うべきはXML_Beautifierを使用することで、 オリジナルのファイルのファイル名および 新しいファイル用のファイル名を渡すことです。

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 />";

そうすればあなたの文書はいい感じに見えます。

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