编程语言
首页 > 编程语言> > php-使用DomDocument将实体添加到DOCTYPE

php-使用DomDocument将实体添加到DOCTYPE

作者:互联网

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

<?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进行此操作.到目前为止,我目前的代码是…

    $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

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

任何帮助,将不胜感激.

基本上,我正在寻求将

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

例:

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

输出:

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

标签:php,xml,doctype,domdocument
来源: https://codeday.me/bug/20191011/1896281.html