php 用php读取xml文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17917539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
read xml file with php
提问by KMC
I have an XML file in this format
我有一个这种格式的 XML 文件
"note.xml"
<currencies>
<currency name="US dollar" code_alpha="USD" code_numeric="840" />
<currency name="Euro" code_alpha="EUR" code_numeric="978" />
</currencies>
PHP CODE
PHP代码
$xml=simplexml_load_file("note.xml");
echo $xml->name. "<br>"; --no output
echo $xml->code_alpha. "<br>"; --no output
echo $xml->code_numeric . "<br>"; --no output
print_r($xml);
output of print_r($xml)-->SimpleXMLElement Object ( [currency] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => US dollar [code_alpha] => USD [code_numeric] => 840 ) )
print_r($xml)-->SimpleXMLElement 对象的输出 ( [currency] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => US Dollar [code_alpha] => USD [code_numeric] => 840 ) )
I didnt get any output for the ECHO statements I tried 'simplexml_load_file' and tried reading from it but it doesnt work. Please tell me what php code should I use to read from this format of XML file.
我没有得到 ECHO 语句的任何输出,我尝试了 'simplexml_load_file' 并尝试从中读取,但它不起作用。请告诉我应该使用什么 php 代码来读取这种格式的 XML 文件。
回答by Prix
Using DomDocument:
使用 DomDocument:
<?php
$str = <<<XML
<currencies>
<currency name="US dollar" code_alpha="USD" code_numeric="840" />
<currency name="Euro" code_alpha="EUR" code_numeric="978" />
</currencies>
XML;
$dom = new DOMDocument();
$dom->loadXML($str);
foreach($dom->getElementsByTagName('currency') as $currency)
{
echo $currency->getAttribute('name'), "\n";
echo $currency->getAttribute('code_alpha'), "\n";
echo $currency->getAttribute('code_numeric'), "\n";
echo "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n";
}
?>
Using simplexml:
使用 simplexml:
<?php
$str = <<<XML
<currencies>
<currency name="US dollar" code_alpha="USD" code_numeric="840" />
<currency name="Euro" code_alpha="EUR" code_numeric="978" />
</currencies>
XML;
$currencies = new SimpleXMLElement($str);
foreach($currencies as $currency)
{
echo $currency['name'], "\n";
echo $currency['code_alpha'], "\n";
echo $currency['code_numeric'], "\n";
echo "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n";
}
?>
回答by shamcs
You can use DomDocument to achieve this objective.
您可以使用 DomDocument 来实现此目标。
Check on this post http://www.developersnote.com/2013/12/how-to-read-xml-file-in-php.html
检查这篇文章http://www.developersnote.com/2013/12/how-to-read-xml-file-in-php.html
$objDOM = new DOMDocument();
//Load xml file into DOMDocument variable
$objDOM->load("../configuration.xml");
//Find Tag element "config" and return the element to variable $node
$node = $objDOM->getElementsByTagName("config");
//looping if tag config have more than one
foreach ($node as $searchNode) {
$dbHost = $searchNode->getAttribute('host');
$dbUser = $searchNode->getAttribute('userdb');
$dbPass = $searchNode->getAttribute('dbpass');
$dbDatabase = $searchNode->getAttribute('database');
}