xml操作的几种方法
判断你的xml文件是否合法。创建XX.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
<!--
#showError {
color: #F00;
font-size: 16px;
}
-->
</style></head>
<script type="text/javascript">
function validateXML(filename){
var txt="";
if (window.ActiveXObject){
document.getElementById("showError").innerText ="";
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.validateOnParse= true;
xmlDoc.load(filename);
if(xmlDoc.parseError.errorCode!=0){
txt="Error Code: " + xmlDoc.parseError.errorCode + "\n";
txt=txt+"Error Reason: " + xmlDoc.parseError.reason ;
txt=txt+"Error Line: " + xmlDoc.parseError.line;
}else{
txt="没有错误";
}
document.getElementById("showError").innerText = txt;
}else{
alert("此浏览器不支持验证!");
}
}
</script>
<body>
<center>
<h2>通过DTD验证XML文件</h2>
<p>
<input id="xmlfile" type="text" />
<input type="button" name="submit" value="验 证" onClick="validateXML(document.getElementById('xmlfile').value);" />
</p>
</center>
<div id="showError"></div>
</body>
</html>
1使用css定义xml .创建table_demo.css
book
{
display: block;
border-width: 1px;
border-color: #930;
border-style:outset;
background-color: #CCC
}
name
{
display: block;
text-align: center;
}
publisher,company,author,ISBN,price
{
text-align: center;
width: 18%
}
url
{
display: block;
}
创建table_demo.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="table_demo.css"?>
<books>
<book>
<name>《C#从入门到精通(第2版)》</name>
<publisher>清华大学出版社</publisher>
<author>王小科</author>
<company>明日科技</company>
<ISBN>9787302226628</ISBN>
<price unit="RBM" >69.80</price>
<url><![CDATA[http://www.mingribook.com/bookinfo.php?id=227&sid=4]]></url>
</book>
<books>
2使用xsl定义xml。创建style.xsl
<?xml version='1.0' encoding='gb2312'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>使用XSL显示XML文档</title>
<style>
td {
font-size: 9pt; color: #000000;
}
body {
margin: 0px;
}
.tableBorder {
border: #aaaaaa 1px solid
}
</style>
</head>
<body>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0"
background="Images/bg.jpg" class="tableBorder">
<tr>
<td width="26%" height="112" valign="top"> </td>
</tr>
<tr>
<td height="248" align="center" valign="top"><table width="90%" border="1"
bordercolor="#FFFFFF" bordercolordark="#FFFFFF" bordercolorlight="#999999"
cellpadding="0" cellspacing="0">
<tr height="27">
<td align="center">书号</td>
<td align="center">书名</td>
<td align="center">作者</td>
<td align="center">出版社</td>
</tr>
<xsl:for-each select="books/book">
<tr height="27">
<td><xsl:value-of select="ISBN"/></td>
<td><xsl:value-of select="bookname"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="publishing"/></td>
</tr>
</xsl:for-each>
</table></td>
</tr>
<tr>
<td height="69" valign="top"> </td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
创建index.xml
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet href="style.xsl" type="text/xsl"?>
<books>
<book>
<ISBN>7-115-14547-4</ISBN>
<bookname>JSP数据库系统开发完全手册</bookname>
<author>王国辉、李文立 杨亮</author>
<publishing>人民邮电出版社</publishing>
</book>
<book>
<ISBN>7-115-14689-6</ISBN>
<bookname>JSP数据库系统开发案例精选</bookname>
<author>王国辉、王易</author>
<publishing>人民邮电出版社</publishing>
</book>
</books>
3使用dtd文件定义xml文件。创建simple_demo.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT book (name,publisher,company,author,ISBN,price,url)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT url (#PCDATA)>
创建xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book SYSTEM "simple_demo.dtd">
<book>
<name>《C#从入门到精通(第2版)》</name>
<publisher>清华大学出版社</publisher>
<company>明日科技</company>
<author>王小科</author>
<ISBN>9787302226628</ISBN>
<price>69.80</price>
<url><![CDATA[http://www.mingribook.com/bookinfo.php?id=227&sid=4]]></url>
</book>
<!ELEMENT book (name,publisher,company,author+,ISBN,price,url)>定义重复元素
<!ELEMENT book (name,publisher,company,author,ISBN,price,url,(tel|phone))>声明选择性元素
<!ELEMENT books (book+)> 定义多本书
<!ENTITY info "软件工程师入门丛书" > 定义实体 <type>&info;</type> 应用实体