XML 个文件的字典
Dictionary by XML file
我们有这样一个 XML 字典文件:
<?xml version="1.0" encoding="UTF-8"?>
<words>
<word>
<phrase lang="IR">حسابدار</phrase>
<phrase lang="FR">Comptable</phrase>
<phrase lang="CN">会计</phrase>
<phrase lang="US">Accountant</phrase>
</word>
<word>
<phrase lang="IR">حسابرس</phrase>
<phrase lang="FR">Auditeur</phrase>
<phrase lang="CN">核数师</phrase>
<phrase lang="US">Auditor</phrase>
</word>
<word>
<phrase lang="IR">مهندس</phrase>
<phrase lang="FR">Ingénieur</phrase>
<phrase lang="CN">工程师</phrase>
<phrase lang="US">Engineer</phrase>
</word>
</words>
我们需要一个 PHP 代码来查找包含 "ginee"(工程师)和法语翻译(工程师)的 return 的文本。
我们的PHP代码是
<?php
$xml = simplexml_load_file("test.xml");
$nodes = $xml->xpath('//word/phrase[contains(..,"ngin")]');
var_dump($nodes);
?>
它return
array(4) { [0]=> object(SimpleXMLElement)#2 (2) { ["@attributes"]=> array(1) { ["lang"]=> string(2) "IR" } [0]=> string(10) "مهندس" } [1]=> object(SimpleXMLElement)#3 (2) { ["@attributes"]=> array(1) { ["lang"]=> string(2) "FR" } [0]=> string(10) "Ingénieur" } [2]=> object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(1) { ["lang"]=> string(2) "CN" } [0]=> string(9) "工程师" } [3]=> object(SimpleXMLElement)#5 (2) { ["@attributes"]=> array(1) { ["lang"]=> string(2) "US" } [0]=> string(8) "Engineer" } }
由于您已经从使用的查询中获得了所需的节点,只需使用 ->attributes()
:
访问 属性(法语 FR
)
基本思路:
$nodes = $xml->xpath('//word/phrase[contains(..,"ngin")]');
$lang_needed = 'FR';
$result = '';
if(!empty($nodes)) {
foreach($nodes as $node) {
if((string) $node->attributes()->lang === $lang_needed) {
// $result = (string) $node;
echo $node;
}
}
}
您也可以这样设计查询:
$lang_needed = 'FR';
$nodes = $xml->xpath("//word/phrase[contains(..,'A')][@lang = '{$lang_needed}']");
if(!empty($nodes as $node)) {
// $result = (string) $node;
echo $node;
}
这是一个class,将XML解析成一个结构
class Simple_Parser
{
var $parser;
var $error_code;
var $error_string;
var $current_line;
var $current_column;
var $data = array();
var $datas = array();
function parse($data)
{
$this->parser = xml_parser_create('UTF-8');
//UTF-8 encoding geoffers_
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
xml_set_character_data_handler($this->parser, 'cdata');
if (!xml_parse($this->parser, $data))
{
$this->data = array();
$this->error_code = xml_get_error_code($this->parser);
$this->error_string = xml_error_string($this->error_code);
$this->current_line = xml_get_current_line_number($this->parser);
$this->current_column = xml_get_current_column_number($this->parser);
}
else
{
$this->data = $this->data['child'];
}
xml_parser_free($this->parser);
}
function tag_open($parser, $tag, $attribs)
{
$this->data['child'][$tag][] = array('data' => '', 'attribs' => $attribs, 'child' => array());
$this->datas[] =& $this->data;
$this->data =& $this->data['child'][$tag][count($this->data['child'][$tag])-1];
}
function cdata($parser, $cdata)
{
$this->data['data'] .= $cdata;
}
function tag_close($parser, $tag)
{
$this->data =& $this->datas[count($this->datas)-1];
array_pop($this->datas);
}
}
我们有这样一个 XML 字典文件:
<?xml version="1.0" encoding="UTF-8"?>
<words>
<word>
<phrase lang="IR">حسابدار</phrase>
<phrase lang="FR">Comptable</phrase>
<phrase lang="CN">会计</phrase>
<phrase lang="US">Accountant</phrase>
</word>
<word>
<phrase lang="IR">حسابرس</phrase>
<phrase lang="FR">Auditeur</phrase>
<phrase lang="CN">核数师</phrase>
<phrase lang="US">Auditor</phrase>
</word>
<word>
<phrase lang="IR">مهندس</phrase>
<phrase lang="FR">Ingénieur</phrase>
<phrase lang="CN">工程师</phrase>
<phrase lang="US">Engineer</phrase>
</word>
</words>
我们需要一个 PHP 代码来查找包含 "ginee"(工程师)和法语翻译(工程师)的 return 的文本。
我们的PHP代码是
<?php
$xml = simplexml_load_file("test.xml");
$nodes = $xml->xpath('//word/phrase[contains(..,"ngin")]');
var_dump($nodes);
?>
它return
array(4) { [0]=> object(SimpleXMLElement)#2 (2) { ["@attributes"]=> array(1) { ["lang"]=> string(2) "IR" } [0]=> string(10) "مهندس" } [1]=> object(SimpleXMLElement)#3 (2) { ["@attributes"]=> array(1) { ["lang"]=> string(2) "FR" } [0]=> string(10) "Ingénieur" } [2]=> object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(1) { ["lang"]=> string(2) "CN" } [0]=> string(9) "工程师" } [3]=> object(SimpleXMLElement)#5 (2) { ["@attributes"]=> array(1) { ["lang"]=> string(2) "US" } [0]=> string(8) "Engineer" } }
由于您已经从使用的查询中获得了所需的节点,只需使用 ->attributes()
:
FR
)
基本思路:
$nodes = $xml->xpath('//word/phrase[contains(..,"ngin")]');
$lang_needed = 'FR';
$result = '';
if(!empty($nodes)) {
foreach($nodes as $node) {
if((string) $node->attributes()->lang === $lang_needed) {
// $result = (string) $node;
echo $node;
}
}
}
您也可以这样设计查询:
$lang_needed = 'FR';
$nodes = $xml->xpath("//word/phrase[contains(..,'A')][@lang = '{$lang_needed}']");
if(!empty($nodes as $node)) {
// $result = (string) $node;
echo $node;
}
这是一个class,将XML解析成一个结构
class Simple_Parser
{
var $parser;
var $error_code;
var $error_string;
var $current_line;
var $current_column;
var $data = array();
var $datas = array();
function parse($data)
{
$this->parser = xml_parser_create('UTF-8');
//UTF-8 encoding geoffers_
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
xml_set_character_data_handler($this->parser, 'cdata');
if (!xml_parse($this->parser, $data))
{
$this->data = array();
$this->error_code = xml_get_error_code($this->parser);
$this->error_string = xml_error_string($this->error_code);
$this->current_line = xml_get_current_line_number($this->parser);
$this->current_column = xml_get_current_column_number($this->parser);
}
else
{
$this->data = $this->data['child'];
}
xml_parser_free($this->parser);
}
function tag_open($parser, $tag, $attribs)
{
$this->data['child'][$tag][] = array('data' => '', 'attribs' => $attribs, 'child' => array());
$this->datas[] =& $this->data;
$this->data =& $this->data['child'][$tag][count($this->data['child'][$tag])-1];
}
function cdata($parser, $cdata)
{
$this->data['data'] .= $cdata;
}
function tag_close($parser, $tag)
{
$this->data =& $this->datas[count($this->datas)-1];
array_pop($this->datas);
}
}