使用 DomDocument 将实体添加到 DOCTYPE
Adding entities to the DOCTYPE using DomDocument
我正在尝试创建一个看起来像这样的 XML 文档...
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<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.
<span style="color: #ffff00"><strong>Update </strong></span>
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 " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
DOM 不是用于构建文档类型定义的接口,这就是为什么您找不到将实体声明等内容添加到内部子集的方法的原因。如果必须内联它而不是使用外部子集,则必须将其作为完整字符串提供并相应地加载它。
示例:
$xml = <<<'XML'
<!DOCTYPE stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<NewsPost/>
XML;
$dom = new DOMDocument();
$dom->loadXML($xml);
echo $dom->saveXML();
输出:
<?xml version="1.0"?>
<!DOCTYPE stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<NewsPost/>
我正在尝试创建一个看起来像这样的 XML 文档...
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<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.
<span style="color: #ffff00"><strong>Update </strong></span>
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 " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
DOM 不是用于构建文档类型定义的接口,这就是为什么您找不到将实体声明等内容添加到内部子集的方法的原因。如果必须内联它而不是使用外部子集,则必须将其作为完整字符串提供并相应地加载它。
示例:
$xml = <<<'XML'
<!DOCTYPE stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<NewsPost/>
XML;
$dom = new DOMDocument();
$dom->loadXML($xml);
echo $dom->saveXML();
输出:
<?xml version="1.0"?>
<!DOCTYPE stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<NewsPost/>