精华区 [关闭][返回]

当前位置:月光软件>>讨论区精华>>〖软件开发〗>>● ASP>>★ASP的同类★>>XML>>ASP 与 XML (二)

主题:ASP 与 XML (二)
发信人: dongbao(爱, 是永恒)
整理人: dongbao(2001-03-10 11:45:40), 站内信件
XML Parser如何解析XML檔案呢?
 

XML Parser(XMLDOM)用以解析XML檔案,可於IE(client)與 ASP(server)上使用。

載入XML檔案

載入XML檔案時,XML Parser可使用的屬性與方法如下:

l        Load方法:載入一個XML檔案。

l        xml:可取得XML檔案所有內容。

l        parseError.errorCode:可以得知是否載入錯誤。

l        parseError.Line:得知錯誤解析行數。

l        parseError.errorCode:得知錯誤碼,若不等於0表示有錯誤。

l        parseError.reason:得知錯誤解析原因。

l        parseError.Line:得知錯誤解析行數。

l        parseError.srcText:得知錯誤解析文字。

譬如asp程式碼xmldom1.asp如下:

<%

'create the XML doc

Set xmlDoc = CreateObject("Microsoft.XMLDOM")



'load the XML doc

XMLFile = Server.MapPath("books0.xml")

xmlDoc.Load(XMLFile)

Response.Write "
xmlDoc.parseError.errorCode = " & xmlDoc.parseError.errorCode

Response.Write "
xmlDoc.parseError.Line = " & xmlDoc.parseError.Line

Response.Write "
xmlDoc.parseError.reason = " & xmlDoc.parseError.reason

Response.Write "
xmlDoc.parseError.srcText = " & xmlDoc.parseError.srcText

Response.Write "
xmlDoc.xml = " & xmlDoc.xml

%>

首先使用Set xmlDoc = CreateObject("Microsoft.XMLDOM")指令,以使用XML Parser,再使用Load方法以載入一個XML檔案,由parseError.errorCode可以得知是否載入錯誤,若有錯誤,由parseError.Line得知錯誤解析行數,由parseError.reason得知錯誤解析原因,由parseError.Line得知錯誤解析行數,由parseError.srcText得知錯誤解析文字。

載入後,由xmlDoc.xml可取得XML檔案所有內容。

其中所載入的books0.xml檔案內容如下:

<?xml version="1.0" encoding="BIG5" standalone="yes" ?> 

<bookstore>

  <book genre="網站設計">

    <title>ASP網站熱門應用技術</title>

    <author>

      <first-name>世雄</first-name>

      <last-name>周</last-name>

    </author>

    <price>580</price>

    <publisher>華彩</publisher>

  </book>

  <book genre="網站設計">

    <title>微軟熱門Web技術速成班</title>

    <author>

      <first-name>世雄</first-name>

      <last-name>周</last-name>

    </author>

    <price>450</price>

    <publisher>松崗</publisher>

  </book>

  <book genre="網頁設計">

    <title>動態網頁設計 99 招 PART 2</title>

    <title>動態網頁設計 99 招 PART 3</title>

    <author>

      <first-name>世雄</first-name>

      <last-name>周</last-name>

    </author>

    <price>430</price>

    <publisher>第三波</publisher>

  </book>

</bookstore>

解析XML檔案

解析XML檔案時,XML Parser可使用的屬性與方法如下:

n            selectSingleNode("xxx"):由此點往下找尋名稱為xxx的單一節點。

n            selectNodes("yyy"):由此點往下找尋所有名稱為yyy的節點。

n            length:節點數目。

n            Attributes.getNamedItem("zzz"):某一節點Attribute為zzz的值,譬如” <book genre="網頁設計">”中genre之Attributes.getNamedItem("genre")值為"網頁設計"。

n            nodeTypedValue:某一節點的值。

譬如asp程式碼如下:

<%

'get the node

Set xml_node = xmlDoc.selectSingleNode("bookstore")



'find the book nodes

Set xml_subNodes = xml_node.selectNodes("book")



Response.Write "
xml_subNodes.length = " & xml_subNodes.length

For i = 0 To xml_subNodes.length - 1



'find the genre attribute and get the value

Set xml_subNode = xml_subNodes(i).Attributes.getNamedItem("genre")

Response.Write "<P>genre = " & xml_subNode.nodeValue

 

              'find the title nodes

          Set xml_subNode = xml_subNodes(i).selectNodes("title")

 

              'iterate through the node list

          For j = 0 To xml_subNode.length - 1

           Response.Write "
 title= " & xml_subNode(j).nodeTypedValue

          Next 

 

              'find the author nodes

          Set xml_subNode1 = xml_subNodes(i).selectSingleNode("author")

 

         'find the first-name nodes

          Set xml_subNode2 = xml_subNode1.selectSingleNode("first-name")

      firstname =  xml_subNode2.nodeTypedValue

 

              'find the last-name nodes

          Set xml_subNode2 = xml_subNode1.selectSingleNode("last-name")

      lastname = xml_subNode2.nodeTypedValue

       Response.Write "
 author = " & lastname & firstname

 

              'find the publisher nodes

          Set xml_subNode = xml_subNodes(i).selectSingleNode("publisher")

       Response.Write "
 publisher = " & xml_subNode.nodeTypedValue

 

              'find the price nodes

          Set xml_subNode = xml_subNodes(i).selectSingleNode("price")

       Response.Write "
 price = " & xml_subNode.nodeTypedValue

Next

%>

首先使用selectSingleNode("bookstore")由起點往下找尋名稱為bookstore的單一節點,接著使用selectNodes("book")由此點往下找尋所有名稱為book的節點。

由length得知找到的節點數目。

然後由Attributes.getNamedItem("genre")取得此節點Attribute為genre的值。

再由selectNodes("title")找尋名稱為title的節點,接著由nodeTypedValue取得此節點的值。依序由找尋名稱為author、first-name、last-name、publisher、price節點的值。

由For i = 0 To xml_subNodes.length - 1...Next取得所有book節點的值。

新增加節點

新增加XML檔案的節點時,XML Parser可使用的屬性與方法如下:

l        loadXML(“yyy”):新增加XML標記“yyy”。

l        createElement("xxx"):新增加名稱為xxx的XML節點。

l        documentElement.appendChild(xml_node):將新增加的節點加入XML文件。

l        documentElement.lastChild:移到最後節點處。

l        createAttribute("zzz"):新增加名稱為zzz的Attribute。

l        Text:設定Attribute的值。

l        Attributes.setNamedItem(xml_node):將新增加的Attribute加入XML文件。

譬如asp程式碼如下:

<%

'create the XML doc

Set xmlDoc = CreateObject("Microsoft.XMLDOM")



'load the XML doc

XMLFile = Server.MapPath("log.xml")

xmlDoc.Load(XMLFile)



'check if xml exists in the file

If xmlDoc.xml = "" Then

Call xmlDoc.loadXML("<root></root>")

End If

 

'create the new log node

Set xml_node = xmlDoc.createElement("log")

 

'add the node to the document

Set xml_node = xmlDoc.documentElement.appendChild(xml_node)

 

'get the new node

Set xml_logNode = xmlDoc.documentElement.lastChild

            

'create the new user atribute

Set xml_node = xmlDoc.createAttribute("User")

xml_node.Text = "jack"       

'add the attribute to the list

Call xml_logNode.Attributes.setNamedItem(xml_node)

 

'create the new date atribute

Set xml_node = xmlDoc.createAttribute("Date")

xml_node.Text = CStr(date)       

'add the attribute to the list

Call xml_logNode.Attributes.setNamedItem(xml_node)

 

Set fs = CreateObject("Scripting.FileSystemObject")

Set a = fs.CreatetextFile(XMLFile, True)

a.Write xmlDoc.xml

a.Close

 

Response.Redirect "log.xml"

%>

於瀏覽器執行結果如下,顯示此新產生的XML檔案。

首先使用Set xmlDoc = CreateObject("Microsoft.XMLDOM")指令,以使用XML Parser,再使用Load方法以載入一個XML檔案log.xml,若xmlDoc.xml為空字串表示無內容,則由xmlDoc.loadXML("<root></root>")加入<root></root>標記。

接著由 xmlDoc.createElement("log")以新增加名稱為log的XML節點,由documentElement.appendChild(xml_node)將新增加的節點加入XML文件,再由documentElement.lastChild移到最後節點處以再加以新的節點。

由createAttribute("User")新增加名稱為User的Attribute,由Text設定Attribute的值,由Attributes.setNamedItem(xml_node)將新增加的Attribute加入XML文件。

依序加入User、Date的Attribute。

最後使用Scripting.FileSystemObject之CreatetextFile方法,將此XML文件儲存成一個XML檔案test1.xml,最後Response.Redirect重新導向到此新產生的XML檔案。



----
    春日,阳光明媚的校园,她穿着那一袭纯白的连衣裙,沐浴着 
阳光,漫步于一片新绿之间。在金色的光芒中,她那乌黑的长发随风 
飘逸,那苍白的脸颊已被喜悦的红晕所遮掩。 
   我心翩翩 
   我是天使 
   是你的天使 
   我爱 
   我将要来你身边 
   与你共舞 
   ……  
 

[关闭][返回]






转载请注明:转载自 月光程序代码网 [ http://www.moon-soft.com ]