Goals
In this session you will:
- Understand the DOM
- Use the DOM to manipulate the XML document, add and delete elements
POST
- CSS uses selectors to match elements to properties. What does XSLT use?
- What is one of the theoretical advantages of XSL Formatting/Flow Objects over CSS?
- In the expression "
sort-by="+/-element" , what does the + and - signify?
Understanding the DOM
- DOM provides generic access to DOM-compliant documents: add, edit, delete, manipulate
- DOM is language-independent
- The DOM is based on a tree view of your document. Nodes! Nodes! Nodes!
- DOM useful for CSS, HTML, XML
- DOM + client-side scripting + HTML = DHTML
- DOM components
There is a cheat sheet you can print in landscape mode.
- Document top-level view of the document, with access to all nodes (including root element)
- createElement method - creates an element node
- createAttribute method - creates an attribute node
- createComment method - creates a comment node
- getDocumentElement method - returns root element
- appendChild method - appends a child node
- getChildNodes method - returns child nodes
- Node represents a node - "A node is a reference to an element, its attributes, or text from the document."
- cloneNode method - duplicates a node
- getNodeName method - returns the node name
- getNodeName method - returns the node's name
- getNodeType method - returns the node's type
- getNodeValue method - returns the node's value
- getParentNode method - returns the node's parent's name
- hasChildNodes method - true if has child nodes
- insertBefore method - stuffs child in before specified child
- removeChild method - removes the child node
- replaceChild method - replaces one child with another
- setNodeValue method - sets node's value
- attribute represents an attribute node -
- getAttribute method - gets attribute!
- getTagName method - gets element's name
- removeAttribute method - deletes it
- setAttribute method - sets att's value
Creating an XML document object
- JavaScript:
var myXO = new ActiveXObject("Microsoft.XMLDOM")
- VBScript:
set myXO = CreateObject("Microsoft.XMLDOM")
- .asp:
set myXO = Server.CreateObject("Microsoft.XMLDOM")
- perl:
Adding in a new element
var link = document.createElement('a');
link.setAttribute('href', 'mypage.htm');
Where to put this new element?
locating a slot in the document
- by location:
document.childNodes[1].childNodes[0]
""Find the main document element (HTML), and find its second child (BODY), then look for its first child (DIV)" "
- by ID:
document.getElementById('myDiv').appendChild(txt);
Hiding an element
document.childNodes[1].childNodes[1].childNodes[0].style.display = "none"; (""=show)
Loading an XML document object into the parser
<script language="JavaScript"> (start javascript)
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") (create an instance of the document object)
xmlDoc.async="false" (wait until the document is loaded)
xmlDoc.load("petstore.xml") (load the doc into the
// ....... process the document in here somewhere
</script> (close up the javascript)
manually loading XML into the parser
<script language="JavaScript">
// load up variable var with some xml
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>"
// now create the DO
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(text)
// ....... process the document
</script>
parseError object
document.write(xmlDoc.parseError.property)
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
Traversing nodes
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
Calling XML nodes by name
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
document.write(xmlDoc.getElementsByTagName("from").item(0).text)
Hands on!
- build a simple XML page to simplify the process (root + three children with different names and different content)
- build a DTD to validate the document
- validate
- start an html page to contain our javascript
- jiggle IE preferences to show all errors
- extract and print out the name of the root element
- extract and print out the name[s] of the child[ren] (will require an iteration)
- add an element
- delete an element
Conclusion
XML definitions, manually created XML (elements, attributes) from data, validated with DTD, transformed with XSLT, manipulated the DOM.
Where to go from here
- other coursework
- outside work
- SAX (simple API for xml) in place of DOM (particularly for large, read-only XML docs)
- XPath, Xlink
- Certifications
admin
- clean out your working directories, archive
- book reviews
- book draw
- final roll
- sign certs / assessments
http://www.mousetrap.net/syllabus/xml/day4.html
$Id: day4.orb,v 1.2 2001/10/31 03:20:30 mouse Exp $
Remember, your login is based on your machine's hostname, not on any other number.
~/[initials] refers to the subdirectory under your homedir, named after your initials. Everything except for .dotfiles will be stored in your ~/[initials] directory.