我正在尝试将<Host></Host>
的值保存到我的php中的变量中并遇到问题。这里有很多例子,但似乎没有一个例子可以帮助我做一个单一的价值而且让我感到困惑。
<CONFIG application="SAM" version="3.4.2">
<Database>
<Driver>MYSQL</Driver>
<Host>testdomain.com</Host>
<Port>3306</Port>
<Database>SAMDB2</Database>
<Username>root</Username>
<Password>xxxxx</Password>
</Database>
</CONFIG>
感谢任何帮助
答案 0 :(得分:0)
SimpleXML非常容易解决这个问题:
$xml = <<<XML
<CONFIG application="SAM" version="3.4.2">
<Database>
<Driver>MYSQL</Driver>
<Host>testdomain.com</Host>
<Port>3306</Port>
<Database>SAMDB2</Database>
<Username>root</Username>
<Password>xxxxx</Password>
</Database>
</CONFIG>
XML;
# load the string into simpleXML
$sxe = simplexml_load_string($xml);
# echo the host:
echo "Host: " . $sxe->Database->Host . PHP_EOL;
# echo the driver, port no., and database:
echo "Driver: " . $sxe->Database->Driver
. "; Port: " . $sxe->Database->Port
. "; Database: " . $sxe->Database->Database . PHP_EOL;
# save the host as a object
$host = $sxe->Database->Host;
echo "\nhost object: ";
var_dump($host);
# save the host as a string
$host_str = (string)$sxe->Database->Host;
echo "\nhost string: ";
var_dump($host_str);
输出:
Host: testdomain.com
Driver: MYSQL Port: 3306; Database: SAMDB2
host object: class SimpleXMLElement#3 (1) {
public ${0} =>
string(14) "testdomain.com"
}
host string: string(14) "testdomain.com"