使用DomDocument将实体添加到DOCTYPE [英] Adding entities to the DOCTYPE using DomDocument

查看:70
本文介绍了使用DomDocument将实体添加到DOCTYPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个看起来像这样的XML文档...

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>

我很想使用PHP来做到这一点。到目前为止,我当前拥有的代码是...

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);

您会注意到没有代码可以将标签添加到!DOCTYPE

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

我正在网上寻找信息,却看不到这样做的最佳实践方法。我真的不想诉诸于将XML保存为字符串,然后执行字符串替换(这总是很麻烦)

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.

基本上,我想将

<!DOCTYPE stylesheet>

标记到

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


推荐答案

DOM不是用于构建文档类型的接口定义,这就是为什么您找不到用于将诸如实体声明之类的内容添加到内部子集的方法的原因。如果必须内联而不是使用外部子集,则必须将其作为完整字符串提供并相应地加载。

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.

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

这篇关于使用DomDocument将实体添加到DOCTYPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆