PHP 操作 XML 文件:读取、显示

80酷酷网    80kuku.com

  本例中的php文件读取、显示xml文件内容

以下为php文件的内容,把该php文件和slashdot.xml放在同一个文件夹即可

<?php
$open_tags
= array(
    
'STORY' => '<STORY>'
,
    
'TITLE' => '<TITLE>'
,
    
'URL' => '<URL>'
,
'AUTHOR'=>
'<AUTHOR>'
);
$close_tags
= array(
    
'STORY' => '</STORY>'
,
    
'TITLE' => '</TITLE>'
,
    
'URL' => '</URL>'
,
'AUTHOR'=>
'</AUTHOR>'
);
?>

<?php
//下面就是定义函数来提取数据:

// 处理开始标记的属性指
// $attrs是一个多维数组,键值为属性名, 值就是该属性的值
function startElement($parser, $name, $attrs=''
){
    global
$open_tags, $temp, $current_tag
;
    
$current_tag = $name
;
    if (
$format = $open_tags[$name
]){
    switch(
$name
){
        case
'STORY'
:
        echo
'新的故事: '
;
        break;
        default:
        break;
    }
    }
}

// $current_tag告诉我们正在处理的标记,我们随后会在characterData函数中使用
//
// 当遇到</STORY>标记时我们知道要flush所有的临时变量准备操作下一个标记
<lt;/STORY>'
,
    
'TITLE' => '</TITLE>'
,
    
'URL' => '</URL>'
,
'AUTHOR'=>
'</AUTHOR>'
);
?>

<?php
//下面就是定义函数来提取数据:

// 处理开始标记的属性指
// $attrs是一个多维数组,键值为属性名, 值就是该属性的值
function startElement($parser, $name, $attrs=''
){
    global
$open_tags, $temp, $current_tag
;
    
$current_tag = $name
;
    if (
$format = $open_tags[$name
]){
    switch(
$name
){
        case
'STORY'
:
        echo
'新的故事: '
;
        break;
        default:
        break;
    }
    }
}

// $current_tag告诉我们正在处理的标记,我们随后会在characterData函数中使用
//
// 当遇到</STORY>标记时我们知道要flush所有的临时变量准备操作下一个标记
function endElement($parser, $name, $attrs=''
){
    global
$close_tags, $temp, $current_tag
;
    if (
$format = $close_tags[$name
]){
    switch(
$name
){
        case
'STORY'
:
        
return_page($temp
);
        
$temp = ''
;
        break;
        default:
        break;
    }
    }
}

// 传送给此函数的是元素间的数据
// 例如,对<TITLE>Title Here</TITLE>,$data就等于'Title Here'
function characterData($parser, $data
){
    global
$current_tag, $temp, $catID
;
    switch(
$current_tag
){
    case
'TITLE'
:
        
$temp['title'] = $data
;
        
$current_tag = ''
;
        break;
    case
'URL'
:
        
$temp['url'] = $data
;
        
$current_tag = ''
;
        break;
case
'AUTHOR'
:
     
$temp['author'] = $data
;
        
$current_tag = ''
;   
    default:
        break;
    }
}
?>  


<?php

function return_page
(){
    global
$temp
;
    echo
'o <A HREF="'.$temp['url'].'">'.$temp['title'].'</A>
'
;
echo
'Author:'.$temp['author'].'
'
;
echo
'-----------------------------'
;
echo
'
'
;
}

// 分析的内容
$xml_file = 'slashdot.xml'
;

// 定义字符集,默认是UTF-8
$type = 'UTF-8'
;

// 建立解析器
$xml_parser = xml_parser_create($type
);

// 设置解析选项
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true
);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'
);

// 告诉PHP发现元素时要调用什么函数
// 这些函数同时也处理元素的属性
xml_set_element_handler($xml_parser, 'startElement','endElement'
);

//告诉PHP对字符数据调用什么函数
xml_set_character_data_handler($xml_parser, 'characterData'
);

if (!(
$fp = fopen($xml_file, 'r'
))) {
    die(
"无法打开 $xml_file 文件进行解析!n"
);
}

// 通过循环来解析整个文件
while ($data = fread($fp, 4096
)) {
    if (!(
$data = utf8_encode($data
))) {
        echo
'ERROR'."n"
;
    }
    if (!
xml_parse($xml_parser, $data, feof($fp
))) {
        die(
sprintf( "XML error: %s at line %dnn"
,
        
xml_error_string(xml_get_error_code($xml_parser
)),
        
xml_get_current_line_number($xml_parser
)));
    }
}

xml_parser_free($xml_parser
);

?>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: