2014-11-25 76 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