我正在尝试创建一个看起来像这样的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 "€">
]>
答案 0 :(得分:3)
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/>