将使用 SimpleXML 的代码移植到 PHP 中的 Dom

发布于 2024-08-17 19:05:06 字数 1495 浏览 4 评论 0原文

我有一个扩展 PDO 的类,使其能够从 xml 文件中提取配置。不幸的是,我们的托管提供商禁用了 SimpleXML,因此我需要重构我的代码以使用可用的 Dom。

我正在使用以下代码:

class xmlPDO extends PDO
{
    public function __construct($xml_uri){
        $xml = simplexml_load_file($xml_uri);
        $dsn_template = "%s:host=%s; dbname=%s";
        $dsn = sprintf($dsn_template, $xml->dsn->driver, $xml->dsn->host, $xml->dsn->dbname);
        parent::__construct($dsn, $xml->username, $xml->password);
    }
}

要阅读以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE database [
<!ELEMENT database (name, description, dsn, username, password)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT description (#PCDATA)>
    <!ELEMENT dsn (driver, dbname, host)>
        <!ELEMENT driver (#PCDATA)>
        <!ELEMENT dbname (#PCDATA)>
        <!ELEMENT host (#PCDATA)>
    <!ELEMENT username (#PCDATA)>
    <!ELEMENT password (#PCDATA)>
]>

<database>
    <name>Test Database</name>
    <description>Localhost test database, use only for testing</description>
    <dsn>
        <driver>mysql</driver>
        <dbname>test</dbname>
        <host>localhost</host>
    </dsn>
    <username>user</username>
    <password>test</password>
</database>

我正在查看 php.net 文档,并且很难弄清楚我应该如何执行此操作。我似乎无法提供任何清晰的代码示例来提取这样的数据。

I have a class that extends PDO to give it the ability to pull the configuration from an xml file. Unfortunately the our hosting provider has disabled SimpleXML so I need to refactor my code to use Dom, which is available.

I am using the following code:

class xmlPDO extends PDO
{
    public function __construct($xml_uri){
        $xml = simplexml_load_file($xml_uri);
        $dsn_template = "%s:host=%s; dbname=%s";
        $dsn = sprintf($dsn_template, $xml->dsn->driver, $xml->dsn->host, $xml->dsn->dbname);
        parent::__construct($dsn, $xml->username, $xml->password);
    }
}

To read the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE database [
<!ELEMENT database (name, description, dsn, username, password)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT description (#PCDATA)>
    <!ELEMENT dsn (driver, dbname, host)>
        <!ELEMENT driver (#PCDATA)>
        <!ELEMENT dbname (#PCDATA)>
        <!ELEMENT host (#PCDATA)>
    <!ELEMENT username (#PCDATA)>
    <!ELEMENT password (#PCDATA)>
]>

<database>
    <name>Test Database</name>
    <description>Localhost test database, use only for testing</description>
    <dsn>
        <driver>mysql</driver>
        <dbname>test</dbname>
        <host>localhost</host>
    </dsn>
    <username>user</username>
    <password>test</password>
</database>

I am looking at the php.net documentation and am having a really hard time trying to figure out how I am supposed to be doing this. I can't seem to be any clear code examples of pulling out data like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

始终不够 2024-08-24 19:05:06

您的托管提供商是否禁用了 SimpleXML,或者您刚刚禁用了 SimpleXML 从 URL 加载文件的功能?后者比前者更为常见,如果是后者的话,DomDocument 可能也会遭受同样的命运。

在您深入研究 DomDocument(这是一个功能强大但文档记录很少的 API)之前,请尝试使用curl 函数下载 XML,然后使用 simplexml_load_string 创建您的 SimpleXML 对象。

如果您最终不得不走 DomDocument 路线,您需要搜索 xpath 教程。这是从 DomDocument 对象获取信息的最直接的方法。

Has your hosting provider disabled SimpleXML, or have thye just disabled the ability of SimpleXML to load files from URLs? The later is far more common than the former, and if it's the later DomDocument may suffer the same fate.

Before you dive too deep into DomDocument, which is a robust put poorly documented API, trying using the curl functions to download your XML, and then use simplexml_load_string to create your SimpleXML object.

If you end up having to go the DomDocument route, you want to search around for xpath tutorials. It's the most straightforward way to get information out of a DomDocument object.

浅笑轻吟梦一曲 2024-08-24 19:05:06
<?php

$dom = new DOMDocument();
$dom->load( 'file.xml' );
$xp = new DOMXpath($dom);

$username = $xp->evaluate('//username');
$password = $xp->evaluate('//password');

if ( $username->length > 0 ) {
    echo $username->item(0)->nodeValue;
}

if ( $password->length > 0 ) {
    echo $password->item(0)->nodeValue;
}
<?php

$dom = new DOMDocument();
$dom->load( 'file.xml' );
$xp = new DOMXpath($dom);

$username = $xp->evaluate('//username');
$password = $xp->evaluate('//password');

if ( $username->length > 0 ) {
    echo $username->item(0)->nodeValue;
}

if ( $password->length > 0 ) {
    echo $password->item(0)->nodeValue;
}
燕归巢 2024-08-24 19:05:06

由于您已经有了 DTD 设置,最重要的是向各种标记添加 IDENTITY 属性,然后使用 getElementById

如果您不介意总是处理节点列表集合您始终可以使用getElementsByTagName

否则,您必须使用遍历函数和元素属性(nextSiblingprevSiblingchildNodes)来遍历 DOM。您还可以使用 xpath 查询来简化遍历。

Since you already have a DTD setup the esiest thing to do would be to add and IDENTITY attribute to the various tags and then use getElementById

IF you dont mind always dealing with nodelist collections you can always use getElementsByTagName.

Otherwise you have to use the traversal functions and element properties (nextSibling, prevSibling, childNodes) to traverse the DOM. You could also use xpath queries which would simplyfy the traversal.

少年亿悲伤 2024-08-24 19:05:06

或者我可以使用 simple xml for php 4 类,其行为几乎与simple-xml,但在禁用 simple-xml 时有效。

Or I could use the simple xml for php 4 class that behaves pretty much as simple xml, but works when simple-xml is disabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文