谷歌地图的php domxml错误

时间:2013-10-18 17:28:31

标签: php xml map

我的服务器PHP版本5.1.6 domxml启用但是当我尝试执行

// Start XML file, create parent node
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);

它返回fatel错误

Call to undefined function domxml_new_doc() 

我的服务器php信息是

    dom

DOM/XML enabled
DOM/XML API Version 20031129
libxml Version  2.6.26
HTML Support    enabled
XPath Support   enabled
XPointer Support    enabled
Schema Support  enabled
RelaxNG Support enabled

2 个答案:

答案 0 :(得分:2)

有两种常见的方法可以做到这一点。

<强> DOMDocument

DOM XML被弃用以支持这一点。它遵循W3C规范,因此您可以使用典型的DOM方法,例如getElementsByTagNamegetElementByIdappendChildremoveChild等。它还使用适当的类,例如{{1} },DOMElementDOMNode

DOMNodeList

如果你已经知道如何在Javascript中遍历DOM,你几乎已经知道如何使用DOMDocument和其他相关类在PHP中完成它。

我不知道为什么Shankar的答案被低估了,除非有人赞成采用下一种方法。

<强> SimpleXML

SimpleXML尝试通过仅使用两个类并使用父子结构来遍历文档来实现其名称。

$doc = new DOMDocument('1.0','utf-8');
$root = $doc->appendChild($doc->createElement('markers'));

// output
echo $doc->saveXML();

如果一个元素被视为一个字符串,它本身会对其内容求值,并且可以访问属性,就像访问一个关联数组的元素一样。

$doc = new SimpleXMLElement('<markers/>');

// output
echo $doc->asXML();

如果您不需要挂起对元素的引用,也可以执行此操作。但是,您无法使用此方法添加多个属性,因此您仍然需要先前添加新属性而不遍历整个文档。

$marker = $doc->addChild('marker');
$marker->addAttribute('color','red');

访问您的元素和属性,如下所示:

$doc->addChild('marker')->addAttribute('color','red');

设置元素值只需设置它。

// red
echo $doc->marker[0]['color'];

答案 1 :(得分:0)

为什么不使用 DOMDocument

更改您的代码

$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);

$doc = new DOMDocument('1.0', 'iso-8859-1');
$node = $doc->createElement("markers");
$parnode = $doc->appendChild($node);