SlideShare a Scribd company logo
2
Most read
5
Most read
9
Most read
DOM and SAX Jussi Pohjolainen TAMK University of Applied Sciences
DOM and SAX DOM and SAX Platform and language-independent APIs for manipulating or reading XML-documents API: Application Programming Interface, set of functions, procedures, methods, classes and interfaces DOM and SAX is implemented in most programming languages: Java, PHP..
Differences between DOM and SAX DOM SAX Standardization W3C Recommendation No formal specification Manipulation Reading and Writing (manipulation) Only Reading Memory Consumption Depends on the size of the source xml-file, can be large Very low XML handling Tree-based Event-based
SAX
Overview of SAX SAX: Simple API for XML Originally a Java – only API Nowdays SAX is supported in almost all programming languages Uses a event-driven model Quantity of memory usage is low Only for reading xml-documents
Event-driven? SAX uses event-driven model for reading xml-documents The basic idea is, that SAX parser reads the xml-document " one line at a time". Handler functions reacts when finding elements and other parts of the xml-document. When the parser finds starting tag, then a certain function is called.. when the parser winds ending tag a certain function is called
Example (Wikipedia) <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <RootElement param=&quot;value&quot;> <FirstElement> Some Text </FirstElement> <SecondElement param2=&quot;something&quot;> Pre-Text <Inline>Inlined text</Inline> Post-text. </SecondElement> </RootElement>
Example (Wikipedia) XML Processing Instruction, named xml, with attributes version equal to &quot;1.0&quot; and encoding equal to &quot;UTF-8&quot; XML Element start, named RootElement, with an attribute param equal to &quot;value&quot; XML Element start, named FirstElement XML Text node, with data equal to &quot;Some Text&quot; (note: text processing, with regard to spaces, can be changed) XML Element end, named FirstElement ....
PHP and SAX // Creates an XML Parser  $xml_parser = xml_parser_create();  // Set up for reading  xml_set_element_handler($xml_parser, &quot;startElement&quot;, &quot;endElement&quot;);  xml_set_character_data_handler($xml_parser, &quot;characterData&quot;);  // Open XML file  if (!($fp = fopen($file, &quot;r&quot;))) {  die(&quot;could not open XML input&quot;);  }  // Reading and Parsing xml-file  while ($data = fread($fp, 4096)) {  if (!xml_parse($xml_parser, $data, feof($fp))) {  die(sprintf(&quot;XML error: %s at line %d&quot;,  xml_error_string(xml_get_error_code($xml_parser)),  xml_get_current_line_number($xml_parser)));  }  }  xml_parser_free($xml_parser);
PHP and SAX function startElement($parser, $name, $attrs)  {  // Do something  }  function endElement($parser, $name)  {  // Do something  }  function characterData($parser, $data)  {  echo $data;  }
Benefits of SAX Excellent API when just reading the contents of the XML – file Easy and clean API Does not require much resources (mobile devices!)
DOM
DOM The Document Object Model (DOM) is a platform- and language-independent standard object model for representing HTML or XML and related formats. W3C Recommendation Can be used for manipulating XML – documents Different versions: DOM 1, DOM 2 and DOM 3
Basic Idea behind DOM API for  manipulating  XML – documents DOM loads xml-document into memory and creates a tree-model of the xml-data. Can consume memory, if documents are large
Tree and Nodes Tree consists of  nodes Node can be Element (Element) Text (Text) Attribute (Attr) CDATA (CDATASection) Comment (Comment) Etc
Nodes and Relationships Node has references to it's first child (firstChild) last child (lastChild) next sibling (nextSibling) previous sibling (previousSibling) parent (parentNode
Node's contents Some nodes have contents (nodeValue) Attribute's value Element's value (text) Comment's value (text) etc
Collections NodeList (List of nodes) length item ( index ) NamedNodeMap (List of attributes) getNamedItem( name ) item ( index )
Example using PHP DOM // Load the xml - document $dom = new domDocument(); $dom->load(&quot;books.xml&quot;); // NodeList of name-elements $listOfNodes = $dom->getElementsByTagName(&quot;name&quot;); // Browse all nodes foreach($listOfNodes as $node) { print $node->nodeValue; }
Example using PHP DOM // Load xml-document $dom = new domDocument();  $dom->load(&quot;books.xml&quot;);  // Create element <book></book>  $book = $dom->createElement(&quot;book&quot;);  // create element <title>some contents</title>  $title = $dom->createElement(&quot;title&quot;, $_GET['title']);  // <book><title>some contents</title></book>  $book->appendChild($title);  // Add the book under root element of &quot;books.xml&quot; $dom->documentElement->appendChild($book);  // save $dom->save(&quot;books.xml&quot;);
Removing element $elements = $dom->getElementsByTagname(&quot;kirja&quot;);  $element  = $elements->item(0);  $children = $element->childNodes();  $child  = $element->removeChild( $children->item(0) );  <kirjat>  <kirja>  <nimi>Tuntematon Sotilas</nimi>  </kirja>  <kirja>  <nimi>Learn Java</nimi>  </kirja>  </kirjat>
PHP DOM and Encoding PHP DOM uses utf-8 internally Everything you put into xml-document using PHP DOM must be converted to utf-8. utf8_encode(..), utf8_decode(...)

More Related Content

PPTX
Design pattern-presentation
PPTX
UNIT IV RESOURCE MANAGEMENT AND SECURITY
PDF
Introduction to Design Pattern
PPT
Ooad ch 2
PPTX
Query processing strategies in distributed database
PPTX
Distributed file system
PPTX
Introduction to ajax
Design pattern-presentation
UNIT IV RESOURCE MANAGEMENT AND SECURITY
Introduction to Design Pattern
Ooad ch 2
Query processing strategies in distributed database
Distributed file system
Introduction to ajax

What's hot (20)

PPTX
XML, DTD & XSD Overview
PDF
Major and Minor Elements of Object Model
PPT
PDF
JSON Schema Design
PPTX
Java Server Pages(jsp)
PPT
Introduction to ADO.NET
PPT
Xml parsers
PPT
Ontology engineering
PPTX
Gof design patterns
PPTX
Delegates and events in C#
PPTX
Domain Driven Design
PPT
XML Schema
PDF
Servlet and servlet life cycle
PPT
Java: GUI
PDF
The CAP Theorem
PPT
Data Storage In Android
PDF
Querying XML: XPath and XQuery
PPTX
Xml namespace
PPTX
Ado.Net Tutorial
XML, DTD & XSD Overview
Major and Minor Elements of Object Model
JSON Schema Design
Java Server Pages(jsp)
Introduction to ADO.NET
Xml parsers
Ontology engineering
Gof design patterns
Delegates and events in C#
Domain Driven Design
XML Schema
Servlet and servlet life cycle
Java: GUI
The CAP Theorem
Data Storage In Android
Querying XML: XPath and XQuery
Xml namespace
Ado.Net Tutorial
Ad

Similar to DOM and SAX (20)

PPT
Java XML Parsing
PPT
PDF
Understanding Sax
PPT
PPT
XML and Web Services with PHP5 and PEAR
PPT
Sax Dom Tutorial
PDF
X Usax Pdf
PPT
Processing XML with Java
PPT
PPTX
Unit iv xml dom
PDF
Understanding Dom
PDF
Processing XML
PPT
[DSBW Spring 2010] Unit 10: XML and Web And beyond
PPTX
XML and Web Services
PPTX
WEB PRORAMMING NOTES WITH EXAMPLE PROGRAMS
PPTX
Web data management (chapter-1)
PDF
Building XML Based Applications
Java XML Parsing
Understanding Sax
XML and Web Services with PHP5 and PEAR
Sax Dom Tutorial
X Usax Pdf
Processing XML with Java
Unit iv xml dom
Understanding Dom
Processing XML
[DSBW Spring 2010] Unit 10: XML and Web And beyond
XML and Web Services
WEB PRORAMMING NOTES WITH EXAMPLE PROGRAMS
Web data management (chapter-1)
Building XML Based Applications
Ad

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
PDF
Java Web Services
PDF
Box2D and libGDX
PDF
libGDX: Screens, Fonts and Preferences
PDF
libGDX: Tiled Maps
PDF
libGDX: User Input and Frame by Frame Animation
PDF
Intro to Building Android Games using libGDX
PDF
Advanced JavaScript Development
PDF
Introduction to JavaScript
PDF
Introduction to AngularJS
PDF
libGDX: Scene2D
PDF
libGDX: Simple Frame Animation
PDF
libGDX: Simple Frame Animation
PDF
libGDX: User Input
PDF
Implementing a Simple Game using libGDX
PDF
Building Android games using LibGDX
PDF
Android Threading
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
PDF
Creating Games for Asha - platform
PDF
Intro to Asha UI
Moved to Speakerdeck
Java Web Services
Box2D and libGDX
libGDX: Screens, Fonts and Preferences
libGDX: Tiled Maps
libGDX: User Input and Frame by Frame Animation
Intro to Building Android Games using libGDX
Advanced JavaScript Development
Introduction to JavaScript
Introduction to AngularJS
libGDX: Scene2D
libGDX: Simple Frame Animation
libGDX: Simple Frame Animation
libGDX: User Input
Implementing a Simple Game using libGDX
Building Android games using LibGDX
Android Threading
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Games for Asha - platform
Intro to Asha UI

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
A Presentation on Artificial Intelligence
PDF
Getting Started with Data Integration: FME Form 101
PDF
Machine learning based COVID-19 study performance prediction
PDF
Electronic commerce courselecture one. Pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
1. Introduction to Computer Programming.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Machine Learning_overview_presentation.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
Big Data Technologies - Introduction.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
A Presentation on Artificial Intelligence
Getting Started with Data Integration: FME Form 101
Machine learning based COVID-19 study performance prediction
Electronic commerce courselecture one. Pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Mobile App Security Testing_ A Comprehensive Guide.pdf
1. Introduction to Computer Programming.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Network Security Unit 5.pdf for BCA BBA.
“AI and Expert System Decision Support & Business Intelligence Systems”
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25-Week II
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Machine Learning_overview_presentation.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Group 1 Presentation -Planning and Decision Making .pptx

DOM and SAX

  • 1. DOM and SAX Jussi Pohjolainen TAMK University of Applied Sciences
  • 2. DOM and SAX DOM and SAX Platform and language-independent APIs for manipulating or reading XML-documents API: Application Programming Interface, set of functions, procedures, methods, classes and interfaces DOM and SAX is implemented in most programming languages: Java, PHP..
  • 3. Differences between DOM and SAX DOM SAX Standardization W3C Recommendation No formal specification Manipulation Reading and Writing (manipulation) Only Reading Memory Consumption Depends on the size of the source xml-file, can be large Very low XML handling Tree-based Event-based
  • 4. SAX
  • 5. Overview of SAX SAX: Simple API for XML Originally a Java – only API Nowdays SAX is supported in almost all programming languages Uses a event-driven model Quantity of memory usage is low Only for reading xml-documents
  • 6. Event-driven? SAX uses event-driven model for reading xml-documents The basic idea is, that SAX parser reads the xml-document &quot; one line at a time&quot;. Handler functions reacts when finding elements and other parts of the xml-document. When the parser finds starting tag, then a certain function is called.. when the parser winds ending tag a certain function is called
  • 7. Example (Wikipedia) <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <RootElement param=&quot;value&quot;> <FirstElement> Some Text </FirstElement> <SecondElement param2=&quot;something&quot;> Pre-Text <Inline>Inlined text</Inline> Post-text. </SecondElement> </RootElement>
  • 8. Example (Wikipedia) XML Processing Instruction, named xml, with attributes version equal to &quot;1.0&quot; and encoding equal to &quot;UTF-8&quot; XML Element start, named RootElement, with an attribute param equal to &quot;value&quot; XML Element start, named FirstElement XML Text node, with data equal to &quot;Some Text&quot; (note: text processing, with regard to spaces, can be changed) XML Element end, named FirstElement ....
  • 9. PHP and SAX // Creates an XML Parser $xml_parser = xml_parser_create(); // Set up for reading xml_set_element_handler($xml_parser, &quot;startElement&quot;, &quot;endElement&quot;); xml_set_character_data_handler($xml_parser, &quot;characterData&quot;); // Open XML file if (!($fp = fopen($file, &quot;r&quot;))) { die(&quot;could not open XML input&quot;); } // Reading and Parsing xml-file while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf(&quot;XML error: %s at line %d&quot;, xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser);
  • 10. PHP and SAX function startElement($parser, $name, $attrs) { // Do something } function endElement($parser, $name) { // Do something } function characterData($parser, $data) { echo $data; }
  • 11. Benefits of SAX Excellent API when just reading the contents of the XML – file Easy and clean API Does not require much resources (mobile devices!)
  • 12. DOM
  • 13. DOM The Document Object Model (DOM) is a platform- and language-independent standard object model for representing HTML or XML and related formats. W3C Recommendation Can be used for manipulating XML – documents Different versions: DOM 1, DOM 2 and DOM 3
  • 14. Basic Idea behind DOM API for manipulating XML – documents DOM loads xml-document into memory and creates a tree-model of the xml-data. Can consume memory, if documents are large
  • 15. Tree and Nodes Tree consists of nodes Node can be Element (Element) Text (Text) Attribute (Attr) CDATA (CDATASection) Comment (Comment) Etc
  • 16. Nodes and Relationships Node has references to it's first child (firstChild) last child (lastChild) next sibling (nextSibling) previous sibling (previousSibling) parent (parentNode
  • 17. Node's contents Some nodes have contents (nodeValue) Attribute's value Element's value (text) Comment's value (text) etc
  • 18. Collections NodeList (List of nodes) length item ( index ) NamedNodeMap (List of attributes) getNamedItem( name ) item ( index )
  • 19. Example using PHP DOM // Load the xml - document $dom = new domDocument(); $dom->load(&quot;books.xml&quot;); // NodeList of name-elements $listOfNodes = $dom->getElementsByTagName(&quot;name&quot;); // Browse all nodes foreach($listOfNodes as $node) { print $node->nodeValue; }
  • 20. Example using PHP DOM // Load xml-document $dom = new domDocument(); $dom->load(&quot;books.xml&quot;); // Create element <book></book> $book = $dom->createElement(&quot;book&quot;); // create element <title>some contents</title> $title = $dom->createElement(&quot;title&quot;, $_GET['title']); // <book><title>some contents</title></book> $book->appendChild($title); // Add the book under root element of &quot;books.xml&quot; $dom->documentElement->appendChild($book); // save $dom->save(&quot;books.xml&quot;);
  • 21. Removing element $elements = $dom->getElementsByTagname(&quot;kirja&quot;); $element = $elements->item(0); $children = $element->childNodes(); $child = $element->removeChild( $children->item(0) ); <kirjat> <kirja> <nimi>Tuntematon Sotilas</nimi> </kirja> <kirja> <nimi>Learn Java</nimi> </kirja> </kirjat>
  • 22. PHP DOM and Encoding PHP DOM uses utf-8 internally Everything you put into xml-document using PHP DOM must be converted to utf-8. utf8_encode(..), utf8_decode(...)