2014-11-25 80 views
0

这让我疯狂。我可以复制和粘贴我需要的30分钟前完成的2个值,但是我试图做到“正确”。Simplexml PHP - 获取“深层节点”的值

整个文件中只有一个“用户名”。我如何得到它?我尝试使用xpath($username=$xml->xpath('default_setup/connection/username');),并试图链接节点($ username = $ xml - > {... full path here ...} - > default_setup-> connection-> username;`)。

当我print_r($xml),我可以看到我想要的所有节点及其值。当我print_r($username)我什么都没得到。

<?php 
$xml = simplexml_load_file('database.xml',NULL,LIBXML_NOCDATA); // connection details are inside of CDATA 

$username=$xml->xpath('default_setup/connection/username'); ?> 
<p>Username: <?= (string)$username ?></p><?php // outputs "Array" 

<?php 
foreach($xml as $element) { 
     echo $element . '<br />'; // outputs '<br />' 2 times. 
} 
?> 
<pre> 
Username: 
<?php print_r($username) ?><?php // nothing ?> 
xml: 
<?php print_r($xml) ?><?php // full set of all nodes with everything I need just out of reach ?> 
</pre> 

这里是示例xml。 (它实际上是Magento“app/etc/local.xml”文件)

<default_setup> 
    <connection> 
     <host><![CDATA[localhost]]></host> 
     <username><![CDATA[secret_username]]></username> 
     <password><![CDATA[secret_password]]></password> 
     <dbname><![CDATA[testing_database]]></dbname> 
     <initStatements><![CDATA[SET NAMES utf8]]></initStatements> 
     <model><![CDATA[mysql4]]></model> 
     <type><![CDATA[pdo_mysql]]></type> 
     <pdoType><![CDATA[]]></pdoType> 
     <active>1</active> 
    </connection> 
</default_setup> 
+0

你对这个特殊的问题示例XML?以便我们知道它是什么样子? – Ghost 2014-11-25 02:37:38

+0

@Ghost好的我添加了路径的最后一部分。 ( 'default_setup /连接/用户名')。 – 2014-11-25 02:49:37

回答

0

让我们摆脱你的问题来简化事情。这里是一个例子。

xml文件,使用example.php:

<?php 
$xmlstr = <<<XML 
<?xml version='1.0' standalone='yes'?> 
<movies> 
<movie> 
    <title>PHP: Behind the Parser</title> 
    <characters> 
    <character> 
    <name>Ms. Coder</name> 
    <actor>Onlivia Actora</actor> 
    </character> 
    <character> 
    <name>Mr. Coder</name> 
    <actor>El Act&#211;r</actor> 
    </character> 
    </characters> 
    <plot> 
    So, this language. It's like, a programming language. Or is it a 
    scripting language? All is revealed in this thrilling horror spoof 
    of a documentary. 
    </plot> 
    <great-lines> 
    <line>PHP solves all my web problems</line> 
    </great-lines> 
    <rating type="thumbs">7</rating> 
    <rating type="stars">5</rating> 
</movie> 
</movies> 
XML; 
?> 

PHP代码中提取的情节,你的情况是用户名:

例2获得

<?php 
include 'example.php'; 

$movies = new SimpleXMLElement($xmlstr); 

echo $movies->movie[0]->plot; 
?> 

这来自:http://php.net/manual/en/simplexml.examples-basic.php


多数民众赞成在它发生。

你可以试试这个,特殊值*匹配所有标签。

<?php 
$xml = <<< XML 
<?xml version="1.0" encoding="utf-8"?> 
<books> 
<book>Patterns of Enterprise Application Architecture</book> 
<book>Design Patterns: Elements of Reusable Software Design</book> 
<book>Clean Code</book> 
</books> 
XML; 

$dom = new DOMDocument; 
$dom->loadXML($xml); 
$books = $dom->getElementsByTagName('book'); 
foreach ($books as $book) { 
    echo $book->nodeValue, PHP_EOL; 
} 
?> 

试试看。这里是从php.net的网址:http://php.net/manual/en/domdocument.getelementsbytagname.php

如果你看下面有更多的例子。

+0

你是1级左右。我不得不多走几步。我是否必须为中间节点包含'[0]',即使它们是唯一的节点名称?而且我的目标节点是整个文档中唯一被称为“用户名”的节点,所以为什么我不能只是得到它!?? !!令人沮丧的经历,这。谢谢你的帮助! – 2014-11-25 02:49:02

3

特殊照顾缺少路径链你想要一些节点是:

$conn = $xml->global->resources->default_setup->connection; 
$user = (string) $conn->username; 
$pass = (string) $conn->password; 

你可以在这里使用XPath,但它实际上更多的是麻烦,因为它总是会返回一个节点数组(NodeList中的表现),所以你将不得不循环或使用[0]。它更容易/更易于使用直接访问。这就是说使用XPath做这将是:

$nodes = $xml->xpath('//default_setup/connection'); 
if (count($nodes) > 0) { 
    $conn = $nodes[0]; 
    $user = (string) $conn->username; 
    $pass = (string) $conn->password; 
} 

完整的XML /连接从我进口的情况下,代码可以帮助:

// get path for config 
$config = realpath(dirname(__FILE__) . '/../magento/app/etc/local.xml'); 

if (!$config) { 
    // I've made a terrible mistake. 
    throw new Exception('Could not load magento config.'); 
} else { 
    // load up XML 
    $conf = new SimpleXMLElement($config, LIBXML_NOCDATA, true); 

    // pull the database connection config node 
    $conn = $conf->global->resources->default_setup->connection; 

    // create a DSN - we will worry about manually setting the attributes ourself 
    // since this is a simple one off 
    $dsn = sprintf('mysql:host=%s;dbname=%s', $conn->host, $conn->dbname); 
    $user = (string) $conn->username; 
    $pass = (string) $conn->password; 
    $db = new PDO($dsn, $user, $pass); 


    if (!$db instanceof PDO) { 
     // WOMP, WOMP! 
     throw new Exception('Could not create database connection!'); 
     exit; 
    } 
} 
+0

所以你在说如果有人想要得到'连接'节点,并且它是文档中唯一的这样的节点,那么他们必须包含来自全局的每个节点才能得到它? – 2014-11-25 02:50:30

+0

是的,除非你使用xpath,但正如我在我的答案中所说的xpath在这个特定的实例中更麻烦。 – prodigitalson 2014-11-25 02:51:37

+0

谢谢,你的答案有效。但我想知道我的xpath使用情况是否有问题。我认为我放了一条有效的路,但我似乎没有得到任何东西。为什么我的'print_r'没有显示任何内容? – 2014-11-25 02:55:03