in DOCTYPE using PHP dom I am trying to create an XML document that looks something like this ...

Adding <! ENTITY nbsp ""> in DOCTYPE using PHP dom

I am 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 am trying to do this with PHP. The current code I have so far is ...

    $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 will notice that there is no code to add tags to! DOCTYPE

I have been looking all over the net and cannot see a better way to do this. I really don't want to resort to storing the XML to a string and then replacing the string (which is always a huge bag)

Any help on this would be greatly appreciated.

Basque, I'm looking to turn

<!DOCTYPE stylesheet>

      

in

<!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;">
]>

      

+1


source to share


1 answer


The DOM is not an interface for creating document type definitions, so you will not find methods for adding objects such as entity declarations to an internal subset. If you have to inline it instead of using an external subset, you will have to provide it as a complete string and load it appropriately.


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

      

+2


source







All Articles