我正在使用PHP阅读一些XML,目前正在使用DOMDocument该类来执行此操作.我需要一种方法来获取标签(DOMElement属性)属性的名称和值,而不事先知道它们是什么.该文件似乎并没有提供这样的事.我知道如果我有名字,我可以得到一个属性的值,但同样,我不知道其中任何一个,需要找到它们.
我也知道其他类SimpleXMLElement有这样的能力,但我对它是如何完成的感兴趣DOMDocument.
我试图直观地格式化我的XML文件在输出时的外观.现在,如果你去这里查看源代码,你会看到文件的样子.
我创建该文件的PHP是:(注意,$ links_array是一个url数组)
header('Content-Type: text/xml');
$sitemap = new DOMDocument;
// create root element
$root = $sitemap->createElement("urlset");
$sitemap->appendChild($root);
$root_attr = $sitemap->createAttribute('xmlns');
$root->appendChild($root_attr);
$root_attr_text = $sitemap->createTextNode('http://www.sitemaps.org/schemas/sitemap/0.9');
$root_attr->appendChild($root_attr_text);
foreach($links_array as $http_url){
// create child element
$url = $sitemap->createElement("url");
$root->appendChild($url);
$loc = $sitemap->createElement("loc");
$lastmod = $sitemap->createElement("lastmod");
$changefreq = $sitemap->createElement("changefreq");
$url->appendChild($loc);
$url_text = $sitemap->createTextNode($http_url);
$loc->appendChild($url_text);
$url->appendChild($lastmod);
$lastmod_text = $sitemap->createTextNode(date("Y-m-d"));
$lastmod->appendChild($lastmod_text);
$url->appendChild($changefreq);
$changefreq_text = $sitemap->createTextNode("weekly");
$changefreq->appendChild($changefreq_text);
}
$file = "sitemap.xml";
$fh = fopen($file, 'w') or die("Can't open the sitemap file.");
fwrite($fh, $sitemap->saveXML());
fclose($fh);
} …Run Code Online (Sandbox Code Playgroud) 这让我疯狂......我只想添加另一个img节点.
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<gallery>
<album tnPath="tn/" lgPath="imm/" fsPath="iml/" >
<img src="004.jpg" caption="4th caption" />
<img src="005.jpg" caption="5th caption" />
<img src="006.jpg" caption="6th caption" />
</album>
</gallery>
XML;
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$album = $xmlDoc->getElementsByTagname('album')[0];
// Parse error: syntax error, unexpected '[' in /Applications/XAMPP/xamppfiles/htdocs/admin/tests/DOMDoc.php on line 17
$album = $xmlDoc->getElementsByTagname('album');
// Fatal error: Call to undefined method DOMNodeList::appendChild() in /Applications/XAMPP/xamppfiles/htdocs/admin/tests/DOMDoc.php on line 19
$newImg = $xmlDoc->createElement("img");
$album->appendChild($newImg);
print $xmlDoc->saveXML();
Run Code Online (Sandbox Code Playgroud)
错误:
我正在尝试解析一些不在我服务器上的HTML
$dom = new DOMDocument();
$dom->loadHTMLfile("http://www.some-site.org/page.aspx");
echo $dom->getElementById('his_id')->item(0);
Run Code Online (Sandbox Code Playgroud)
但是php会返回一个类似的错误ID his_id already defined in http://www.some-site.org/page.aspx, line: 33.我认为那是因为DOMDocument正在处理无效的html.那么,即使无效,我怎么解析呢?
如何使用PHP DOM函数更改元素内容?
深入...我已经查询了我的元素,修改了属性,现在想要更改它的内容,我该怎么做?
我有一个大的html文档,有几个图像.我想将所有图像包裹在里面div.wrapped.我该怎么做DOMDocument?
我见过这个appendChild方法,但最后只附加了元素.如何在中间插入一个,然后将图像移入其中?
我正在使用XPath操作一个简短的HTML片段; 当我用$ doc-> saveHTML()输出更改的片段时,DOCTYPE会添加,并且HTML / BODY标签会包装输出.我想删除它们,但只使用DOMDocument函数将所有子项保留在内部.例如:
$doc = new DOMDocument();
$doc->loadHTML('<p><strong>Title...</strong></p>
<a href="http://www....."><img src="http://" alt=""></a>
<p>...to be one of those crowning achievements...</p>');
// manipulation goes here
echo htmlentities( $doc->saveHTML() );
Run Code Online (Sandbox Code Playgroud)
这会产生:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ...>
<html><body>
<p><strong>Title...</strong></p>
<a href="http://www....."><img src="http://" alt=""></a>
<p>...to be one of those crowning achievements...</p>
</body></html>
Run Code Online (Sandbox Code Playgroud)
我尝试了一些简单的技巧,例如:
# removes doctype
$doc->removeChild($doc->firstChild);
# <body> replaces <html>
$doc->replaceChild($doc->firstChild->firstChild, $doc->firstChild);
Run Code Online (Sandbox Code Playgroud)
到目前为止,只删除DOCTYPE并用BODY替换HTML.但是,此时剩下的是body>可变数量的元素.
我如何删除<body>标签但保留其所有子节点,因为它们将以一种干净利落的方式使用PHP的DOM操作进行可变结构化?
我创建了一个像下面的xml
<Request>
<RequestType>Logon</RequestType>
<MobileId>23424</MobileId>
<Password>123456Gg</Password>
</Request>
Run Code Online (Sandbox Code Playgroud)
我的xsd文件就像下面的代码
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Request" type="RequestType"/>
<xsd:complexType name="RequestType">
<xsd:sequence>
<xsd:element name="RequestType">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Logon"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="MobileId" >
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="0" />
<xsd:maxLength value="10" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Password">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="0"/>
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)
我使用PHP'S DOMDocument的schemaValidate函数来验证xml对xsd,并给出以下错误
Fatal Error 4: Start tag expected, '<' not found on line 5
Error 1872: The document has …Run Code Online (Sandbox Code Playgroud) 我对如何重新排序节点有点困惑.我想添加两个简单的"移动项目"和"移动项目"功能.虽然insertBefore()执行我想要移动前一个兄弟之前的兄弟,但是在DOM中移动一个节点的最简单方法是什么?非常感激!
domdocument ×10
php ×9
dom ×5
xml ×4
html ×1
html-parsing ×1
html5 ×1
insertafter ×1
libxml2 ×1
xpath ×1
xsd ×1