[go: up one dir, main page]

0% found this document useful (0 votes)
4 views16 pages

XML2

The document discusses XML attributes, highlighting their limitations compared to child elements, such as the inability to hold multiple values and difficulty in manipulation. It also covers XML comments, tree structure, validation, namespaces, and how to display and parse XML using modern browsers and the XML DOM. Examples are provided to illustrate these concepts, including a sample XML document and JavaScript code for parsing XML.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

XML2

The document discusses XML attributes, highlighting their limitations compared to child elements, such as the inability to hold multiple values and difficulty in manipulation. It also covers XML comments, tree structure, validation, namespaces, and how to display and parse XML using modern browsers and the XML DOM. Examples are provided to illustrate these concepts, including a sample XML document and JavaScript code for parsing XML.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

XML (cont…)

XML Attributes
• XML elements can have attributes. By the use of
attributes we can add the information about the
element.
• XML attributes enhance the properties of the
elements.
• Let us take an example of a book publisher. Here,
book is the element and publisher is the
attribute.
• <book publisher="Tata McGraw Hill"></book>
Why should we avoid XML attributes

• Attributes cannot contain multiple values but child


elements can have multiple values.
• Attributes cannot contain tree structure but child element
can.
• Attributes are not easily expandable. If you want to change
in attribute's vales in future, it may be complicated.
• Attributes cannot describe structure but child elements
can.
• Attributes are more difficult to be manipulated by program
code.
• Attributes values are not easy to test against a DTD, which
is used to define the legal elements of an XML document.
XML Comments
• An XML comment should be written as:
<!-- Write your comment-->
Example:
<?xml version="1.0" encoding="UTF-8" ?>
<!--Students marks are uploaded by months-->
<students>
<student>
<name>Ratan</name>
<marks>70</marks>
</student>
<student>
<name>Aryan</name>
<marks>60</marks>
</student>
</students>
XML Tree Structure
• An XML document has a self descriptive
structure. It forms a tree structure which is
referred as an XML tree. The tree structure makes
easy to describe an XML document.
• A tree structure contains root element (as
parent), child element and so on. It is very easy to
traverse all succeeding branches and sub-
branches and leaf nodes starting from the root.
<?xml version="1.0"?>
<college>
<student>
<firstname>Abhijit</firstname>
<lastname>Saikia</lastname>
<contact>09990449935</contact>
<email>asaikia@abc.com</email>
<address>
<city>Dibrugarh</city>
<state>Assam</state>
<pin>786001</pin>
</address>
</student>
</college>
XML Validation

• A well formed XML document can be validated


against DTD (Document Type Definition) or
Schema.
XML Namespaces

• XML Namespaces provide a method to avoid


element name conflicts.
Solving the Name Conflict Using a Prefix
• <h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

In the example above, there will be no conflict because the two <table>
elements have different names.
Displaying XML

• Raw XML files can be viewed in all major


browsers.
• Don't expect XML files to be displayed as
HTML pages.
Viewing XML Files

• <?xml version="1.0" encoding="UTF-8"?>


- <note>
<to>Abhijit</to>
<from>Jintu</from>
<heading>Reminder</heading>
<body>Don't forget your work!</body>
</note>
• Look at the XML file above in your browser:
note.xml
• Most browsers will display an XML document
with color-coded elements.
XML Parser

• 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.
• All modern browsers have a built-in XML parser
that can convert text into an XML DOM object.
<html>
<body>
<p id="demo"></p>
<script>
var parser, xmlDoc;
var text = "<bookstore><book>" +
"<title>Learning XML </title>" +
"<author>Erik T. Ray</author>" +
"<year>2005</year>" +
"</book></bookstore>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
• A text string is defined:
var text = "<bookstore><book>" +
"<title>Learning XML </title>" +
"<author>Erik T. Ray</author>" +
"<year>2005</year>" +
"</book></bookstore>";

• An XML DOM parser is created:


• parser = new DOMParser();
• The parser creates a new XML DOM object using
the text string:
• xmlDoc = parser.parseFromString(text,"text/xml");

You might also like