XML Writing and Parsing: SOA - Lab2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

XML Writing and

Parsing
SOA – lab2
The Difference Between XML and HTML

XML Usage

Importance of XML
Agenda
XML main components

How to write an XML file?

XML parsing
XML is not a replacement for HTML.

XML and HTML were designed with different goals:

The XML was designed to describe data, with focus on what data is

Difference HTML was designed to display data, with focus on how data
Between XML looks

and HTML HTML is about displaying information, while XML is about


carrying information.

XML is a software- and hardware-independent tool for carrying


information.
XML Usage
• XML can be used to exchange the information
between organizations and systems.
• XML can be used for offloading and reloading of
databases.
• XML can be used to store and arrange the data,
which can customize your data handling needs.
• XML can easily be merged with style sheets to
create almost any desired output.
• Virtually, any type of data can be expressed as
an XML document.
Importance of XML

• XML Separates Data from HTML


• With XML, data can be stored in separate XML files. This way you can concentrate on using
HTML/CSS for display and layout
• XML Simplifies Data Sharing
• In the real world, computer systems and databases contain data in incompatible formats.
• XML data is stored in plain text format. This provides a software- and hardware-independent way
of storing data.
• This makes it much easier to create data that can be shared by different applications.
• XML Simplifies Data Transport
• One of the most time-consuming challenges for developers is to exchange data between
incompatible systems over the Internet.
• Exchanging data as XML greatly reduces this complexity, since the data can be read by different
incompatible applications.
Importance of XML Cont’d

• XML Simplifies Platform Changes


• Upgrading to new systems (hardware or software platforms), is always time consuming. Large amounts of
data must be converted and incompatible data is often lost.
• XML data is stored in text format. This makes it easier to expand or upgrade to new operating systems,
new applications, or new browsers, without losing data.
• XML Makes Your Data More Available
• Different applications can access your data, not only in HTML pages, but also from XML data sources.
• Internet Languages Written in XML
• Several Internet languages are written in XML.
• One of the beauties of XML, is that it can be extended without breaking applications
• XML Declaration :
• The XML document can optionally have an XML
declaration. It is written as follows:
• <?xml version="1.0" encoding="UTF-8"?>
• Where version is the XML version and encoding
specifies the character encoding used in the
document.

XML Main • Tags and Elements


• An XML file is structured by several XML-elements,

Components
also called XML-nodes or XML-tags. The names of
XML-elements are enclosed in triangular brackets
< > as shown below:
• <element>
• Root Element:
<root>
<x>...</x>
<y>...</y>
</root>
• XML Attributes
• An attribute specifies a single
property for the element, using a
XML Main name/value pair. An XML-element
can have one or more attributes.

Components • For example:


• <a

Cont’d
href="http://www.tutorialspoint.
com/">Tutorialspoint!</a>
• Here href is the attribute name
and
http://www.tutorialspoint.co
m/ is attribute value.
• Sometimes ID references are assigned to elements. These IDs
can be used to identify XML elements in much the same way as
the id attribute in HTML.
XML • <messages>

Attributes for <note id="501">


<to>Tove</to>

Metadata <from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
• The id attributes above are for identifying the different notes. It
is not a part of the note itself.
• Metadata (data about data) should be stored as attributes, and
the data itself should be stored as elements.
How to write an XML file?
• The first line in an XML document is called the prolog, it contains XML version, encoding
<?xml version="1.0” encoding="UTF-8" ?>
• Define the root element of your file
• The root element is the first tag you will define in the XML file which will
contain all other elements
• Define the child elements that you will have, the content of each, and
attributes if any
• For Example:
• <contact-info>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
• </contact-info>
XML
Example
XML Example Cont’d
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<!-- This is a comment -->
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
Parse XML in Java

• Use  DOM (Document Object Model)


API  provides the classes to read and
write an XML file.
• DOM parser parses the entire XML file
and creates a  DOM  object in the memory.
• It models an XML file in a  tree
structure  for easy traversal and
manipulation.
• In DOM everything in an XML file is a  node.
Parse XML in Java Cont’d
• Instantiate XML file:  DOM parser loads the
XML file into memory and consider every tag as
an element.
• Get root node:  Document class provides
the  getDocumentElement()  method to get the
root node and the element of the XML file.
• Get all nodes:  The  getElementByTagName()  method
retrieves all the specific tag name from
the XML file.
• Where  ELEMENT_NODE  type refers to a non-text
node which has sub-elements.
• If we need to access all nodes from the
starting, including the root node, we can
recursively call the getChildElement()
method.
Parse XML in Java Cont’d

• Get Node by text value:  We can


use  getElementByTextValue()  method in
order to search for a node by its value.
• Get Node by attribute value:  If we want to
search a node by the value of a specific
attribute, we can use the
getElementByTagName() method along with
getAttribute() method.
• https://developer.mozilla.org/en-
US/docs/Web/XML/XML_introduction
• https://www.javatpoint.com/how-to-read-xml-
file-in-java
References • https://www.journaldev.com/848/java-file-path-
absolute-canonical
• https://www.tutorialspoint.com/java_xml/java_d
om_parse_document.htm

You might also like