2

I'm trying to create an XML document that looks something like this...

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE stylesheet  [
    <!ENTITY nbsp   "&#160;">
    <!ENTITY copy   "&#169;">
    <!ENTITY reg    "&#174;">
    <!ENTITY trade  "&#8482;">
    <!ENTITY mdash  "&#8212;">
    <!ENTITY ldquo  "&#8220;">
    <!ENTITY rdquo  "&#8221;"> 
    <!ENTITY pound  "&#163;">
    <!ENTITY yen    "&#165;">
    <!ENTITY euro   "&#8364;">
]>
<NewsPost>
    <Post>
        <PermaLink>http://news.bradfordastronomy.co.uk/?p=92</PermaLink>
        <Title>Change of Venue for Monday Meetings until March 2015</Title>
        <Content>Due to building work at Eccleshill library, the Monday meetings will be held at     Upper Bolton Conservative Club, Idle Road, Bradford, BD2 4JN.&#13;
&#13;
&nbsp;&#13;
&#13;
&nbsp;&#13;
&#13;
&lt;span style="color: #ffff00"&gt;&lt;strong&gt;Update &lt;/strong&gt;&lt;/span&gt;&#13;
&#13;
The building work is taking longer than expected; however, we hope to be back at the Library by     the end of March 2015.</Content></Post></NewsPost>

I'm tring to do this using PHP. The current code that I have so far is this...

    $imp = new DOMImplementation;

    $dtd = $imp->createDocumentType('stylesheet', '', '');

    $domDoc = new DOMDocument('1.0', 'utf-8');
    $domDoc->preserveWhiteSpace = false;

    require_once(newsFolder.'/wp-blog-header.php'); 
    //global $post;
    $args = array( 'posts_per_page' => 1 );
    $myposts = get_posts( $args );


    $rootElement = $domDoc->createElement('NewsPost');
    $domDoc->appendChild($rootElement); 

    foreach( $myposts as $post ) : setup_postdata($post);
        $postNode = $domDoc->createElement("Post");
        $rootElement->appendChild($postNode);

        $permaLinkNode = $domDoc->createElement("PermaLink",get_permalink());
        $postNode->appendChild($permaLinkNode);

        $titleNode = $domDoc->createElement("Title",get_the_title());
        $postNode->appendChild($titleNode);

        //$contentNode = $domDoc->createElement("Excerpt",get_the_excerpt());
        //$postNode->appendChild($contentNode);

        $contentNode = $domDoc->createElement("Content",get_the_content());
        $postNode->appendChild($contentNode);
    endforeach;

    $domDoc->save(cacheFolder.'LatestWordPressEntry.xml');

    unset($domDoc);

You'll notice that there is no code to add the tags to the !DOCTYPE

I'm looking all over the net and can't see the best practice method of doing this. I really do not want to resort to saving the XML to a string, then doing a string replace (which is always a huge cludge)

Any help on this would be greatly appreciated.

Bascially, I'm looking to turn the

<!DOCTYPE stylesheet>

tag into

<!DOCTYPE stylesheet  [
    <!ENTITY nbsp   "&#160;">
    <!ENTITY copy   "&#169;">
    <!ENTITY reg    "&#174;">
    <!ENTITY trade  "&#8482;">
    <!ENTITY mdash  "&#8212;">
    <!ENTITY ldquo  "&#8220;">
    <!ENTITY rdquo  "&#8221;"> 
    <!ENTITY pound  "&#163;">
    <!ENTITY yen    "&#165;">
    <!ENTITY euro   "&#8364;">
]>
miken32
  • 42,008
  • 16
  • 111
  • 154
Colin Dawson
  • 435
  • 2
  • 12

1 Answers1

3

The DOM isn't an interface for building document type definitions, which is why you won't find methods for adding things like entity declarations to the internal subset. If you must inline it instead of using an external subset, you're going to have to provide it as a complete string and load it up accordingly.


Example:

$xml = <<<'XML'
<!DOCTYPE stylesheet  [
    <!ENTITY nbsp   "&#160;">
    <!ENTITY copy   "&#169;">
    <!ENTITY reg    "&#174;">
    <!ENTITY trade  "&#8482;">
    <!ENTITY mdash  "&#8212;">
    <!ENTITY ldquo  "&#8220;">
    <!ENTITY rdquo  "&#8221;">
    <!ENTITY pound  "&#163;">
    <!ENTITY yen    "&#165;">
    <!ENTITY euro   "&#8364;">
]>
<NewsPost/>
XML;

$dom = new DOMDocument();
$dom->loadXML($xml);

echo $dom->saveXML();

Output:

<?xml version="1.0"?>
<!DOCTYPE stylesheet [
<!ENTITY nbsp "&#160;">
<!ENTITY copy "&#169;">
<!ENTITY reg "&#174;">
<!ENTITY trade "&#8482;">
<!ENTITY mdash "&#8212;">
<!ENTITY ldquo "&#8220;">
<!ENTITY rdquo "&#8221;">
<!ENTITY pound "&#163;">
<!ENTITY yen "&#165;">
<!ENTITY euro "&#8364;">
]>
<NewsPost/>
user3942918
  • 25,539
  • 11
  • 55
  • 67
  • It's not as elegent as I was expecting. Was hoping to be able to do something with the $dtd = $imp->createDocumentType('stylesheet', '', ''); but it seems to be beyond the PHP Dom at the moment. This answer has solved the problem, so I'm happy to run with it. – Colin Dawson Jan 10 '15 at 21:26
  • From the [DOM core documentation](http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-412266927): "The DOM Core does not support editing Notation nodes; they are therefore readonly." – ThW Jan 12 '15 at 12:54