Porting

 

Home
SF Project
SF Download
Javadoc
Example
Porting
Thermopylae
XPath
SF Feedback

This page shows how to port to Sparta from a standard DOM application. Many of the class names and method names are the same and so if you are lucky your code will not have to change much; this page lists the exceptions.

In the following: d is a Document variable, e is an Element variable, t is a Text variable, and n is a Node variable (any of the previous 3).

 
Standard DOM Sparta DOM
import import org.w3c.dom.* import com.hp.hpl.sparta.*
d = DocumentBuilderFactory .newInstance() .newDocumentBuilder() .newDocument() d=new Document() Sparta uses concrete classes
e = d.createElement(tagName) e = new Element(tagName) ditto
t = d.createTextNode(string) t = new Text(string) ditto
e = d.getDocumentElement()
e = (Element)document.getFirstChild()
e = d.getDocumentElement() The W3C provides two different ways of getting the same thing. Sparta provides just one way.
e.getTagName()
e.getNodeName()
e.getTagName() ditto
t.getData()
t.getNodeValue()
t.getData() ditto
if(n.getNodeType()==Node.ELEMENT_NODE) if(n instanceof Element) the W3C DOM is language independent; with Sparta you can use Java type info
NamedNodeMap attrs = e.getAttributes() Enumeration attrNames = e.getAttributeNames() Sparta attributes are not a Node sub-class; instead an element has a set of String name-value pairs.
e.getAttributes().getNamedItem("foo") e.getAttribute("foo") ditto
e.getElementsByTagName(tagName) e.xpathSelectElements("//"+tagName)
SAXException DOMException
d = DocumentBuilderFactory .newInstance() .newDocumentBuilder() .parse(file) d = Parser.parse(file)
DOMSource(d) DOMSource(DocumentImpl.wrapper(d)) To use Sparta with classes such as [javax.xml.transform.dom.DOMSource] which expect W3C documents use the wrapper method of [hp.hpl.agile.thermopylae.DocumentImpl] to covert from Sparta to W3C.
 

SourceForge Logo