xml操作的幾種方法

weixin_30588675發表於2020-04-05

判斷你的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> 應用實體

轉載於:https://www.cnblogs.com/ywnn/archive/2012/11/04/2753628.html

相關文章