xml document object model anthony borquez. the document object model a programming interface for...

14
XML Document Object Model Anthony Borquez

Upload: jason-preston

Post on 02-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

XML Document Object Model

Anthony Borquez

Page 2: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

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.

Page 3: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

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

Page 4: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

• 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

Page 5: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

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

Page 6: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

JavaScript in IE 5.0var xmlDoc = new

ActiveXObject("Microsoft.XMLDOM")

VBScriptset xmlDoc = CreateObject("Microsoft.XMLDOM")

ASPset xmlDoc =

Server.CreateObject("Microsoft.XMLDOM")

Page 7: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

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>

Page 8: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

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>

Page 9: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

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)

Page 10: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

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)

Page 11: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

The parseError Properties

errorCode Returns a long integer error codeReason Returns a string explaining the reason

for the errorLine Returns a long integer representing the

line number for the errorlinePos Returns a long integer representing the

line position for the errorsrcText Returns a string containing the line that

caused the errorurl Returns the url pointing the loaded documentfilePos Returns a long integer file position of

the error

Page 12: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

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

Page 13: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

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).textheader.innerText = nodes.item(2).text body.innerText = nodes.item(3).text

Page 14: XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document

Accessing XML elements by name

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false“xmlDoc.load("note.xml")document.write(xmlDoc.getElementsByTagName("from").i

tem(0).text)