RSS
当前位置 :| 首页>网站建设>PHP编程>

PHP操作XML格式文件的方法概述

来源: 作者: 时间:2008-03-30 Tag: 点击:



<?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>'

本例中的php文件读取、显示xml文件内容以下为php文件的内容,把该php文件和slashdot.xml放在同一个文件夹即可.

,
    
'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><BR>'
;
echo
'Author:'.$temp['author'].'<BR>'
;
echo
'-----------------------------'
;
echo
'<br>'
;
}

// 分析的内容
$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
);

?>

本文出自电脑知识网,不保证资源的正确性与完整性,更多资源请访问电脑知识网


上一篇:PHP+Apache的安装与配置
下一篇:没有了
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
合作站点