1 / 14

XML Document Object Model

XML Document Object Model. Anthony Borquez. The Document Object Model. a programming interface for HTML and XML documents. It defines the way a document can be accessed and manipulated. 

edana
Download Presentation

XML Document Object Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Document Object Model Anthony Borquez

  2. The Document Object Model • a programming interface for HTML and XML documents. It defines the way a document can be accessed and manipulated.  • Using a DOM, a programmer can create a document, navigate its structure, and add, modify, or delete its elements. • The W3C DOM has been designed to be used with any programming language. • provides a standard programming interface that can be used in a wide variety of environments and applications.

  3. The Node Interface • XML parser can be used to load an XML document into the memory of your compute • information can be retrieved and manipulated by accessing the Document Object Model (DOM). • The DOM represents a tree view of the XML document • The documentElement is the top-level of the tree • This element has one or many childNodes that represent the branches of the tree

  4. A Node Interface is used to read and write (or access if you like) the individual elements in the XML node tree • The childNodes property of the documentElement can be accesses with a for/each construct to enumerate each individual node

  5. XML DOM Parser: language-neutral programming model • Supports JavaScript, VBScript, Perl, VB, Java, C++ and more • Supports W3C XML 1.0 and XML DOM • Supports DTD and validation

  6. JavaScript in IE 5.0 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") VBScript set xmlDoc = CreateObject("Microsoft.XMLDOM") ASP set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")

  7. Loading an XML file into the parser <script language="JavaScript"> var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("note.xml") // ....... processing the document goes here </script>

  8. Loading pure XML text into the parser <script language="JavaScript"> var text="<note>“ text=text+"<to>Tove</to><from>Jani</from>“ text=text+"<heading>Reminder</heading>“ text=text+"<body>Don't forget me this weekend!</body>" text=text+"</note>" var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.loadXML(text) // ....... processing the document goes here </script>

  9. The parseError Object var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("ksdjf.xml") document.write("<br>Error Code: ") document.write(xmlDoc.parseError.errorCode) document.write("<br>Error Reason: ") document.write(xmlDoc.parseError.reason) document.write("<br>Error Line: ") document.write(xmlDoc.parseError.line)

  10. XML Error var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("note_error.xml") document.write("<br>Error Code: ") document.write(xmlDoc.parseError.errorCode) document.write("<br>Error Reason: ") document.write(xmlDoc.parseError.reason) document.write("<br>Error Line: ") document.write(xmlDoc.parseError.line)

  11. The parseError Properties errorCode Returns a long integer error code Reason Returns a string explaining the reason for the error Line Returns a long integer representing the line number for the error linePos Returns a long integer representing the line position for the error srcText Returns a string containing the line that caused the error url Returns the url pointing the loaded document filePos Returns a long integer file position of the error

  12. Traversing the node tree set xmlDoc=CreateObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("note.xml") for each x in xmlDoc.documentElement.childNodes document.write(x.nodename) document.write(": ") document.write(x.text) next

  13. Providing HTML content from XML files • XML can separate HTML documents from their data var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("note.xml") nodes = xmlDoc.documentElement.childNodes to.innerText = nodes.item(0).text from.innerText = nodes.item(1).text header.innerText = nodes.item(2).text body.innerText = nodes.item(3).text

  14. Accessing XML elements by name var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false“ xmlDoc.load("note.xml") document.write(xmlDoc.getElementsByTagName("from").item(0).text)

More Related