search
Homephp教程php手册PHP 操作 XML 文件:读取、显示

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

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

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

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

// 处理开始标记的属性指
// $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函数中使用
//
// 当遇到标记时我们知道要flush所有的临时变量准备操作下一个标记
'
,
    
'TITLE' => ''
,
    
'URL' => ''
,
'AUTHOR'=>
''
);
?>

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

// 处理开始标记的属性指
// $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函数中使用
//
// 当遇到标记时我们知道要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 Here,$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;
    }
}
?>  



function return_page
(){
    global
$temp
;
    echo
'o .$temp['url'].'">'.$temp['title'].'
'
;
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
);

?>



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools