Presentation is loading. Please wait.

Presentation is loading. Please wait.

Schema Data Processing

Similar presentations


Presentation on theme: "Schema Data Processing"— Presentation transcript:

1 Schema Data Processing
Unit 10

2 Key Concepts DOM basics DOM components Nodes Node types Node hierarchy

3 Document Object Model In-memory data storage.
Hierarchical data storage model. Allows navigation between nodes. Allows data insertion and data retrieval from the DOM Language-neutral representation of data Supported by .NET languages

4 Some DOM-based parsers

5 DOM Components

6 XML Nodes <root> <name> <first>Joe</first>
<middle/> <last>Smith</last> </name>

7 Node Types Node type Description Document
The document root, which is a container for all document nodes. DocumentFragment Temporary containing holding a subset of document nodes. DocumentType A <!DOCTYPE…> node. EntityReference Entity reference text. Element An element node. Attr An attributed of an element, represented as a node.

8 Node Types (cont'd) Node type Description ProcessingInstruction
Node containing processing instruction information. Comment Comment node. Text Text value of an element or attribute. CDATASection Node representing a CDATA section. Entity DTD <!ENTITY…> declaration. Notation DTD notation declaration.

9 Creating an XML Document
1 // Fig : BuildXml.java 2 // Creates element node, attribute node, comment node, 3 // processing instruction and a CDATA section. 4 5 import java.io.*; 6 import org.w3c.dom.*; 7 import org.xml.sax.*; 8 import javax.xml.parsers.*; 9 import com.sun.xml.tree.XmlDocument; 10 11 public class BuildXml { 12 private Document document; 13 14 public BuildXml() 15 { 16 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 19 try { 21 // get DocumentBuilder DocumentBuilder builder = factory.newDocumentBuilder(); 25

10 Creating an XML Document (cont'd)
// create root node document = builder.newDocument(); } catch ( ParserConfigurationException pce ) { pce.printStackTrace(); } 32 Element root = document.createElement( "root" ); document.appendChild( root ); 35 // add a comment to XML document Comment simpleComment = document.createComment( "This is a simple contact list" ); root.appendChild( simpleComment ); 40 // add a child element Node contactNode = createContactNode( document ); root.appendChild( contactNode ); 44 // add a processing instruction ProcessingInstruction pi = document.createProcessingInstruction( "myInstruction", "action silent" ); root.appendChild( pi ); 50

11 Creating an XML Document (cont'd)
// add a CDATA section CDATASection cdata = document.createCDATASection( 53` "I can add <, >, and ?" ); root.appendChild( cdata ); 55 try { 57 // write the XML document to a file ( (XmlDocument) document).write( new FileOutputStream( "myDocument.xml" ) ); } catch ( IOException ioe ) { ioe.printStackTrace(); } 65 } 66 67 public Node createContactNode( Document document ) 68 { 69 // create FirstName and LastName elements Element firstName = document.createElement( "FirstName" ); Element lastName = document.createElement( "LastName" ); 73 firstName.appendChild( document.createTextNode( "Sue" ) ); lastName.appendChild( document.createTextNode( "Green" ) ); 76

12 Creating an XML Document (cont'd)
// create contact element Element contact = document.createElement( "contact" ); 79 // create an attribute Attr genderAttribute = document.createAttribute( "gender" ); genderAttribute.setValue( "F" ); 83 // append attribute to contact element contact.setAttributeNode( genderAttribute ); contact.appendChild( firstName ); contact.appendChild( lastName ); return contact; 89 } 90 91 public static void main( String args[] ) 92 { BuildXml buildXml = new BuildXml(); 94 } 95 }

13 XML Document Result 1 // Fig. 8.15 : TraverseDOM.java
2 // Traverses DOM and prints various nodes. 3 4 import java.io.*; 5 import org.w3c.dom.*; 6 import org.xml.sax.*; 7 import javax.xml.parsers.*; 8 import com.sun.xml.tree.XmlDocument; 9 10 public class TraverseDOM { 11 private Document document; 12 13 public TraverseDOM( String file ) 14 { try { 16 // obtain the default parser DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating( true ); DocumentBuilder builder = factory.newDocumentBuilder(); 22 // set error handler for validation errors builder.setErrorHandler( new MyErrorHandler() ); 25

14 XML Document Result (cont’d)
// obtain document object from XML document document = builder.parse( new File( file ) ); processNode( document ); } catch ( SAXParseException spe ) { System.err.println( "Parse error: " + spe.getMessage() ); System.exit( 1 ); } catch ( SAXException se ) { se.printStackTrace(); } catch ( FileNotFoundException fne ) { System.err.println( "File \'" file + "\' not found. " ); System.exit( 1 ); } catch ( Exception e ) { e.printStackTrace(); } 46 } 47 48 public void processNode( Node currentNode ) 49 { switch ( currentNode.getNodeType() ) { 51 22 // set error handler for validation errors builder.setErrorHandler( new MyErrorHandler() ); 25

15 XML Document Result (cont’d)
// process a Document node case Node.DOCUMENT_NODE: Document doc = ( Document ) currentNode; 55 System.out.println( "Document node: " + doc.getNodeName() + "\nRoot element: " + doc.getDocumentElement().getNodeName() ); processChildNodes( doc.getChildNodes() ); break; 62 // process an Element node case Node.ELEMENT_NODE: System.out.println( "\nElement node: " + currentNode.getNodeName() ); NamedNodeMap attributeNodes = currentNode.getAttributes(); 69 for ( int i = 0; i < attributeNodes.getLength(); i++){ Attr attribute = ( Attr ) attributeNodes.item( i ); 72 System.out.println( "\tAttribute: " + attribute.getNodeName() + " ; Value = " + attribute.getNodeValue() ); } 77 processChildNodes( currentNode.getChildNodes() ); break; 80 XML Document Result (cont’d)

16 XML Document Result (cont’d)
// process a text node and a CDATA section case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: Text text = ( Text ) currentNode; 85 if ( !text.getNodeValue().trim().equals( "" ) ) System.out.println( "\tText: " + text.getNodeValue() ); break; } 91 } 92 93 public void processChildNodes( NodeList children ) 94 { if ( children.getLength() != 0 ) 96 for ( int i = 0; i < children.getLength(); i++) processNode( children.item( i ) ); 99 } 100 public static void main( String args[] ) { if ( args.length < 1 ) { System.err.println( "Usage: java TraverseDOM <filename>" ); System.exit( 1 ); } 108 TraverseDOM traverseDOM = new TraverseDOM( args[ 0 ] ); } 111 } XML Document Result (cont’d)


Download ppt "Schema Data Processing"

Similar presentations


Ads by Google