Download presentation
Presentation is loading. Please wait.
1
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Chapter VI: Processing XML [ Dr. M’hamed MATAOUI ]
2
Overview of Presentation
Introduction to XML Technologies Fundamental Concepts of XML XML Grammars (DTD, W3C schema) XML document transformations (CSS, XSLT) XPath, XQuery Processing XML DOM SAX Error Handling Overview of Presentation [ Dr. M’hamed MATAOUI ]
3
Structure of an XML document
XML Parsers Tree-Based Parsers (DOM) Tree-based parsers holds the entire document in Memory and transforms the XML document into a Tree structure. It analyzes the whole document, and provides access to the Tree elements (DOM). This type of parser is a better option for smaller XML documents, but not for large XML document as it causes major performance issues. Event-Based Parsers (SAX) Event-based parsers do not hold the entire document in Memory, instead, they read in one node at a time and allow you to interact with in real time. Once you move onto the next node, the old one is thrown away. This type of parser is well suited for large XML documents. It parses faster and consumes less memory. Processing XML [ Dr. M’hamed MATAOUI ]
4
XML Processing APIs JAVA: Python: Delphi: C++: Processing XML
JAXP (Java API for XML parsing) : javax.xml.parsers DOM (Document object model) : org.w3c.dom SAX (Simple API for XML) : org.xml.sax StAX (Streaming API for XML) : javax.xml.stream TrAX (Transformation API for XML): javax.xml.transform ; javax.xml.transform.dom ; javax.xml.transform.sax ; javax.xml.transform.stream Xalan (processeur XSLT) : org.apache.xalan.processor ; org.apache.xalan.templates ; org.apache.xalan.transformer Xpath : org.apache.xpath All major Programming languages (and browsers) have an API (or a built-in) XML parser to access and manipulate XML documents. Python: xml.etree package lxml iterparse() PyXB PyXSD PyXML libxml2dom py-dom-xpath XSLTools Amara 2.x pyxser Delphi: Microsoft provides a COM-based XML parser and DOM, as well as an XSLT engine and a SAX2-compliant reader CUESoft provides a set of native Delphi XML components, including an XML parser and DOM, and an XSLT engine TurboPower provides another set of native Delphi XML components, including an XML parser and DOM, and an XSLT engine (Commercial) Open XML provides a native Delphi parser and DOM - the XDOM package SAX for Pascal is an implementation of the SAX interfaces in Pascal Oxml, … C++: Microsoft provides a COM-based XML parser and DOM, as well as an XSLT engine and a SAX2-compliant reader (MSXML) RapidXML TinyXML CodeSynthesis XSD pugixml libstudxml Apache Xerces-C++ libxml++ … Processing XML [ Dr. M’hamed MATAOUI ]
5
XML Processing APIs: DOM
The XML DOM (Document Object Model) defines the properties and methods for accessing and editing XML. However, before an XML document can be accessed, it must be loaded into an XML DOM object. The DOM defines the objects and properties of all document elements, and the methods (interface) to access them. The DOM defines a standard for accessing and manipulating documents. The XML DOM defines the objects, properties and methods of all XML elements. The XML DOM is: A standard object model for XML A standard programming interface for XML Platform- and language-independent A W3C standard In other words: The XML DOM is a standard for how to get, change, add, or delete XML elements. Processing XML [ Dr. M’hamed MATAOUI ]
6
XML Processing APIs: DOM
The DOM API is generally an easier API to use. It provides a familiar tree structure of objects. The DOM API is ideal for interactive applications because the entire object model is present in memory, where it can be accessed and manipulated by the user. On the other hand, constructing the DOM requires reading the entire XML structure and holding the object tree in memory, so it is much more CPU- and memory-intensive. For that reason, the SAX API tends to be preferred for server-side applications and data filters that do not require an in-memory representation of the data. Processing XML [ Dr. M’hamed MATAOUI ]
7
Structure of an XML document
DOM (Document Object Model) Structure of an XML document How to Address different Parts ? This may be used in Programming! Si on essaye de tracer la representation arborescente d’un document XML en montrant la position d’un nœud donné à l’interieur de l’arbre XML, on aura donc, Sibling : frere (ou enfant de meme parent) À préparer un exemple similaire pour expliquer cet aspect NodeList childNodes = parentNode.getChildNodes(); Node node = childNodes.item(i); Processing XML [ Dr. M’hamed MATAOUI ]
8
Using DOM Object in Programming
Steps to Using DOM : Following are the steps used while parsing a document using DOM Parser. Import XML-related packages. Create a DocumentBuilder Create a Document from a file or stream Extract the root element Examine attributes Examine sub-elements Processing XML [ Dr. M’hamed MATAOUI ]
9
XML Processing APIs : DOM with JAVA
Import the Required Classes: Import XML-related packages. Create a DocumentBuilder Create a Document from a file or stream Extract the root element Examine attributes Examine sub-elements import java.io.File; import java.io.IOException; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.*; Processing XML [ Dr. M’hamed MATAOUI ]
10
XML Processing APIs : DOM with JAVA
Import XML-related packages. Create a DocumentBuilder Create a Document from a file or stream Extract the root element Examine attributes Examine sub-elements DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Processing XML [ Dr. M’hamed MATAOUI ]
11
XML Processing APIs : DOM with JAVA
Import XML-related packages. Create a DocumentBuilder Create a Document from a file or stream Extract the root element Examine attributes Examine sub-elements String XML_path = "src/Processing_XML_with_SAX_and_DOM/" ; Document document = builder.parse(new File(XML_path + "xmlfile_2.xml")); Processing XML [ Dr. M’hamed MATAOUI ]
12
XML Processing APIs : DOM with JAVA
Import XML-related packages. Create a DocumentBuilder Create a Document from a file or stream Extract the root element Examine attributes Examine sub-elements String XML_path = "src/Processing_XML_with_SAX_and_DOM/" ; Document document = builder.parse(new File(XML_path + "xmlfile_2.xml")); Node root = document.getDocumentElement(); Processing XML [ Dr. M’hamed MATAOUI ]
13
XML Processing APIs : DOM with JAVA
Import XML-related packages. Create a DocumentBuilder Create a Document from a file or stream Extract the root element Examine attributes Examine sub-elements NamedNodeMap AttrnodeMap = root.getAttributes(); for (int i = 0; i < AttrnodeMap.getLength(); i++) { Node currentAttr = AttrnodeMap.item(i); System.out.println("Attname:" + currentAttr.getNodeName()) ; System.out.println("AttValue:" + currentAttr.getNodeValue()) ; } Processing XML [ Dr. M’hamed MATAOUI ]
14
XML Processing APIs : DOM with JAVA
Import XML-related packages. Create a DocumentBuilder Create a Document from a file or stream Extract the root element Examine attributes Examine sub-elements // Point the First Child of the root element Node First_child = root.getFirstChild() ; System.out.println("content:" + First_child.getTextContent()) ; Processing XML [ Dr. M’hamed MATAOUI ]
15
XML Processing APIs : DOM with JAVA
Import XML-related packages. Create a DocumentBuilder Create a Document from a file or stream Extract the root element Examine attributes Examine sub-elements // Point the First Chapter Node Node Plan_Node = First_child.getNextSibling() ; Node Chapter_First_Node = Plan_Node.getFirstChild() ; Processing XML [ Dr. M’hamed MATAOUI ]
16
XML Processing APIs : DOM with JAVA
Modify the content of an XML Node. Node Plan_Node = First_child.getNextSibling() ; Node Chapter_First_Node = Plan_Node.getFirstChild() ; Chapter_First_Node.setTextContent("Chapter number 1"); Processing XML [ Dr. M’hamed MATAOUI ]
17
XML Processing APIs : DOM with JAVA
Save Modification in an XML file. // --- Get the Document Builder DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // --- Build Document String XML_path = "src/Processing_XML_with_SAX_and_DOM/" ; Document document = builder.parse(new File(XML_path + "xmlfile_2.xml")); // --- Here are manipulation instructions Transformer transformer = TransformerFactory.newInstance().newTransformer(); Result output = new StreamResult(new File( XML_path + "new_xml_file.xml")); Source input = new DOMSource(document); transformer.transform(input, output); Processing XML [ Dr. M’hamed MATAOUI ]
18
XML Processing APIs : DOM with JAVA
Node Operations : Creating nodes Traversing nodes Searching for nodes Obtaining node content Creating attributes Removing and changing nodes Inserting nodes Processing XML [ Dr. M’hamed MATAOUI ]
19
XML Processing APIs : DOM with JAVA
Other methods and properties. getChildNodes() : example Node root = document.getDocumentElement(); NodeList Nlist = root.getChildNodes() ; Node first_child = Nlist.item(0) ; //----- Access to the first Child getParentNode() getLastChild() getNextSibling() getPreviousSibling() removeChild() … getAttributes() getNodeName() getNodeType() getNodeValue() replaceChild() … hasChildNodes() hasAttributes() … Processing XML [ Dr. M’hamed MATAOUI ]
20
XML Processing APIs : DOM with JAVA
A complete example, See the demo. Comments and questions are welcome Processing XML [ Dr. M’hamed MATAOUI ]
21
XML Processing APIs : SAX with JAVA
SAX requires much less memory than DOM, because SAX does not construct an internal representation (tree structure) of the XML data, as a DOM does. Instead, SAX simply sends data to the application as it is read; your application can then do whatever it wants to do with the data it sees. The Simple API for XML (SAX) is the event-driven, serial-access mechanism that does element-by-element processing. From : Processing XML [ Dr. M’hamed MATAOUI ]
22
XML Processing APIs : SAX with JAVA
The basic outline of the SAX parsing APIs is shown in the Figure. To start the process, an instance of the SAXParserFactory class is used to generate an instance of the parser. The parser wraps a SAXReader object. When the parser's parse() method is invoked, the reader invokes one of several callback methods implemented in the application. Those methods are defined by the interfaces ContentHandler, ErrorHandler, DTDHandler, and EntityResolver. From : Processing XML [ Dr. M’hamed MATAOUI ]
23
XML Processing APIs : SAX with JAVA
Import the Required Classes: Import XML-related packages. ….. import java.io.File ; import java.io.IOException ; import javax.xml.parsers.* ; import org.w3c.dom.* ; import org.xml.sax.* ; Processing XML [ Dr. M’hamed MATAOUI ]
24
XML Processing APIs : SAX with JAVA
Import XML-related packages. Create the SAXParser object SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); //--- or false SAXParser saxParser = factory.newSAXParser(); Processing XML [ Dr. M’hamed MATAOUI ]
25
XML Processing APIs : SAX with JAVA
Import XML-related packages. Create the SAXParser object Create and call the content handler DefaultHandler content_handler; content_handler = new DefaultHandler() { public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { … } public void endElement(String uri, String localName, String qName) throws SAXException { … } public void characters(char ch[], int start, int length) throws SAXException { … } }; saxParser.parse("xml_file.xml", handler); //--- Parse the XML file using the defined handler Processing XML [ Dr. M’hamed MATAOUI ]
26
XML Processing APIs : SAX with JAVA
Handling Errors File xml_file_path = new File("xmlfile.xml"); //---- Specify here the path of your XML file DocumentBuilderFactory xml_file_Instance = DocumentBuilderFactory.newInstance(); xml_file_Instance.setValidating(true); // Default is false DocumentBuilder xml_file_Object = xml_file_Instance.newDocumentBuilder(); ErrorHandler xml_handler = new MyErrorHandler(); xml_file_Object.setErrorHandler(xml_handler); Document doc_1 = xml_file_Object.parse(xml_file_path); Processing XML [ Dr. M’hamed MATAOUI ]
27
XML Processing APIs : SAX with JAVA
A complete example, See the demo. Comments and questions are welcome Processing XML [ Dr. M’hamed MATAOUI ]
28
XML Processing APIs : Parsing Errors
Handling Errors private static class MyErrorHandler implements ErrorHandler { public void warning(SAXParseException e) throws SAXException { … } public void error(SAXParseException e) throws SAXException { … } public void fatalError(SAXParseException e) throws SAXException { … } private void printInfo(SAXParseException e) { … } } Processing XML [ Dr. M’hamed MATAOUI ]
29
XML Processing APIs You can now start programming XML with JAVA
Good Luck with your next Labwork Processing XML [ Dr. M’hamed MATAOUI ]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.