我需要解决各种小类别的数据的显示维护问题,当然用数据库就很容易解决了。但是用xml更方便
而且维护方便(文本文档的先天优势。)
1 | 留学生 |
2 | 大院大所 |
3 | 研发机构 |
4 | 其他 |
php解析xml,用domxml函数库,我在里面转了两天。然而,还是这个代码不用domxml的好用。
至少它通过了,得出了我要的结果。最关键的是取属性名必须大写。如:attrs['ID']
type.xml
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE cyytype[
<!ELEMENT typeindex ANY>
<!ELEMENT item ANY>
<!ELEMENT item_id (#PCDATA)>
<!ELEMENT item_name ANY>
<!NOTATION jpg SYSTEM 'C:\Program Files\Internet Explorer\IEXPLORE.EXE'>
<!ENTITY logo SYSTEM 'logo.jpg' NDATA jpg>
<!ENTITY author 'bailing wu'>
<!ATTLIST item_name
lenth CDATA #IMPLIED
logo ENTITY #IMPLIED
>
<!ATTLIST typeindex
id ID #REQUIRED
>
]>
<cyytype>
<typeindex id="qylb" baseno="first"><!--企业类别-->
<item>
<item_id>1</item_id>
<item_name>留学生</item_name>
<item_test><it_test>test</it_test></item_test>
</item>
<item>
<item_id>2</item_id>
<item_name>大院大所</item_name>
</item>
<item>
<item_id>3</item_id>
<item_name>研发机构</item_name>
</item>
<item>
<item_id>4</item_id>
<item_name>其他</item_name>
</item>
</typeindex>
<typeindex id="qyxz"><!--企业性质-->
<item>
<item_id>1</item_id>
<item_name>外商独资</item_name>
</item>
<item>
<item_id>2</item_id>
<item_name>外商合资</item_name>
</item>
<item>
<item_id>3</item_id>
<item_name>外商合作</item_name>
</item>
</typeindex>
</cyytype>
<!DOCTYPE cyytype[
<!ELEMENT typeindex ANY>
<!ELEMENT item ANY>
<!ELEMENT item_id (#PCDATA)>
<!ELEMENT item_name ANY>
<!NOTATION jpg SYSTEM 'C:\Program Files\Internet Explorer\IEXPLORE.EXE'>
<!ENTITY logo SYSTEM 'logo.jpg' NDATA jpg>
<!ENTITY author 'bailing wu'>
<!ATTLIST item_name
lenth CDATA #IMPLIED
logo ENTITY #IMPLIED
>
<!ATTLIST typeindex
id ID #REQUIRED
>
]>
<cyytype>
<typeindex id="qylb" baseno="first"><!--企业类别-->
<item>
<item_id>1</item_id>
<item_name>留学生</item_name>
<item_test><it_test>test</it_test></item_test>
</item>
<item>
<item_id>2</item_id>
<item_name>大院大所</item_name>
</item>
<item>
<item_id>3</item_id>
<item_name>研发机构</item_name>
</item>
<item>
<item_id>4</item_id>
<item_name>其他</item_name>
</item>
</typeindex>
<typeindex id="qyxz"><!--企业性质-->
<item>
<item_id>1</item_id>
<item_name>外商独资</item_name>
</item>
<item>
<item_id>2</item_id>
<item_name>外商合资</item_name>
</item>
<item>
<item_id>3</item_id>
<item_name>外商合作</item_name>
</item>
</typeindex>
</cyytype>
phpxml.php
<?php
$file = "type.xml";
$currentTag = "";
$item_id = "";
$item_name = "";
$findid='qylb';
$users = array();
//输出数组元素的通用函数,不用print_r
function outarr(&$arr){
foreach($arr as $item=>$value){
echo $item."=".$value."<BR>".chr(13);
}
}
//解析xml开始标记函数
function startElement($parser, $name, $attrs) {
global $currentTag,$find,$findid;
$currentTag = $name;
if($currentTag=="TYPEINDEX" && $attrs['ID']=='qylb'){
outarr(&$attrs);
$find=true;
}
}
//解析xml结束标记函数
function endElement($parser, $name) {
global $items, $item_id,$find, $item_name;
if($name=="TYPEINDEX" && $find==true){
$find=false; //如果到了容器的结束,将标志位设置为false,不再对数组取数。
}
if($find==true){
if(strcmp($name, "ITEM") == 0){
$item_id = trim("$item_id");
$item_name = trim("$item_name");
$items[] = array("ITEM_ID" => $item_id, "ITEM_NAME" => $item_name);
$item_id = "";
$item_name = "";
}
}
}
//解析xml文本标记函数
function characterData($parser, $data) {
global $currentTag, $item_id, $item_name;
switch ($currentTag) {
case"ITEM_ID":
$item_id .= $data;
break;
case"ITEM_NAME":
$item_name .= $data;
break;
default:
}
}
//初始过程
function readUserInfo() {
global $file, $items;
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
return $items;
}
//输出结果信息过程
function printUserInfo($item_id, $item_name) {
print "<tr height='21'>";
print "<td>$item_id</td>";
print "<td>$item_name</td>";
print "</tr>";
}
?>
<html>
<head>
<title>User Infomation</title>
</head>
<body>
<table border="1">
<tr height="21" align="center">
<td width="80" bgcolor="#EEEEEE">Item ID</td>
<td width="100" bgcolor="#EEEEEE">Item NAME</td>
</tr>
<?php
//测试代码
$items = readUserInfo();
$num_rows = count($items);
for($i = 0; $i < $num_rows; $i++) {
printUserInfo($items[$i]["ITEM_ID"], $items[$i]["ITEM_NAME"]);
}
?>
</table>
</body>
</html>
$file = "type.xml";
$currentTag = "";
$item_id = "";
$item_name = "";
$findid='qylb';
$users = array();
//输出数组元素的通用函数,不用print_r
function outarr(&$arr){
foreach($arr as $item=>$value){
echo $item."=".$value."<BR>".chr(13);
}
}
//解析xml开始标记函数
function startElement($parser, $name, $attrs) {
global $currentTag,$find,$findid;
$currentTag = $name;
if($currentTag=="TYPEINDEX" && $attrs['ID']=='qylb'){
outarr(&$attrs);
$find=true;
}
}
//解析xml结束标记函数
function endElement($parser, $name) {
global $items, $item_id,$find, $item_name;
if($name=="TYPEINDEX" && $find==true){
$find=false; //如果到了容器的结束,将标志位设置为false,不再对数组取数。
}
if($find==true){
if(strcmp($name, "ITEM") == 0){
$item_id = trim("$item_id");
$item_name = trim("$item_name");
$items[] = array("ITEM_ID" => $item_id, "ITEM_NAME" => $item_name);
$item_id = "";
$item_name = "";
}
}
}
//解析xml文本标记函数
function characterData($parser, $data) {
global $currentTag, $item_id, $item_name;
switch ($currentTag) {
case"ITEM_ID":
$item_id .= $data;
break;
case"ITEM_NAME":
$item_name .= $data;
break;
default:
}
}
//初始过程
function readUserInfo() {
global $file, $items;
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
return $items;
}
//输出结果信息过程
function printUserInfo($item_id, $item_name) {
print "<tr height='21'>";
print "<td>$item_id</td>";
print "<td>$item_name</td>";
print "</tr>";
}
?>
<html>
<head>
<title>User Infomation</title>
</head>
<body>
<table border="1">
<tr height="21" align="center">
<td width="80" bgcolor="#EEEEEE">Item ID</td>
<td width="100" bgcolor="#EEEEEE">Item NAME</td>
</tr>
<?php
//测试代码
$items = readUserInfo();
$num_rows = count($items);
for($i = 0; $i < $num_rows; $i++) {
printUserInfo($items[$i]["ITEM_ID"], $items[$i]["ITEM_NAME"]);
}
?>
</table>
</body>
</html>