Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Wednesday, 17 January 2018

How to create an XML Schema Using Simple Type Elements

A simple type element in an XML Schema contains only text and cannot have any child elements or attributes. Now create an XML document, simpletypeschema.xml and an XSD file simpletypeschema.xsd to learn how to create an XML Schema.

////Shows the code of the simpletypeschema.xml file //////

<dresses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="simpletypeschema.xsd">
<size>06</size>
<mediumDressSize>12</mediumDressSize>
<smallDressSize>6</smallDressSize>
<eLargeSize>extra large</eLargeSize>
</dresses>

///Shows the code of the simpletypeschema.xsd File////
<xs.schma xmlns:xs="http://ww.w3.org/2001/XMLSchema">
<xs:element name="dresses" type="SizeType">
<xs:complexType name="SizesType">
<xs:choice maxOccurs="unbounded">
<xs:element name="size" type="DressSizeType">
<xs:element name="mediumDressSize" type="MediumDressSizeType">
<xs:element name="smallDressSize" type="SmallDressSizeType">
<xs:element name="eLargeSize" type="eLargeSizeType">
</xs:choice>
</xs:complexType>
<xs:simpleType name="DressSizeType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="2">
<xs:maxInclusive value="18">
<xs:pattern value="\d{1,2}">
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="MediumDressSizeType">
<xs:retriction base="DressSizeType">
<xs:minInclusive value="8">
<xs:maxInclusive value="12">
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="SmallDressSizeType">
<xs:restriction base="DressSizeType">
<xs:minInclusive value="2">
<xs:maxInclusive value="6">
<xs:pattern value="\d{1}">
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="eLargeSizeType">
<xs:restriction base="xs:token">
<xs:enumeration value="small">
<xs:enumeration value="medium"><xs:enumeration value="large">
<xs:enumeration value="extra large">
</xs:restriction>
</xs:simpleType>
</xs:schema>

dresses is a complex type element. It contains various simple type elements, which inclue size,mediumDressSize, smallDressSize, and eLargeSize. The structure of all these simple as well as complex type elements is defined in the simpletypeschema.xsd schema file.





Monday, 15 January 2018

How to specify the reference of a namespace in a DTD file

When an XML document uses a pre-defined DTD file and its element use the reference of a namespace then you can specify the namespace in the DTD file. Now here we create an XML document checkbook.xml and DTD file checkbook.dtd to learn how to specify the reference of a namespace in a DTD file.

Shows the code of the checkbook.xml file.

<?xml version="1.0"?>
<!DOCTYPE checkbook SYSTEM "checkbook.dtd">
<checkbook>
<deposit type="direct-deposit" xmlns="http://namespace.com/checkbook">
<payor>Bob's Bolts</payor>
<amount>987.32</amount>
<date>21-6-2018</date>
<description category="income">Paycheck</description>
</deposit>
</checkbook>


Shows the code of the checkbook.dtd file.

<!ELEMENT checkbook (deposit)*>
<!ELEMENT deposit (payor, amount, date, description?)>
<!ATTLIST deposit type (cash|check|direct-deposit|transfer) #REQUIRED xmlns CDATA #FIXED "http://namespace.com/checkbook">
<!ELEMENT amount (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT payor (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ATTLIST description caategory (cash|entertainment|food|income|work) 'food'>

Here an XML document is created that uses the reference of a DTD file checkbook.dtd, to define the structure of the checkbook.xml file. The code of the checkbook.dtd file. The XML document contains the checkbook element which further contains the deposit element and its child elements. the elements and attributes of the deposit element use the reference of default namespace, http://namespace.com/checkbook.



How to include the reference of multiple namespaces in an XML Document

An XML element can contain the reference of multiple namespaces. Now here we create an XML document named multiplenamespaces.xml to learn how to include the reference of multiple namespaces in an XML document.

/////Shows the code of the multiplenamespaces.xml file/////

<?xml version="1.0"?>
<Song xmlns="http://www.namespace.com/Song"
xmlns:art="http://www.webucator.com/Artist">
<Title>The Girl Is Mine</Title>
<Year>1983</Year>
<Artists>


<art:Artist BirthYear="1955">
<art:Name>
<art:Title>Mr.</art:Title>
<art:FirstName>Anil</art:FirstName>
<art:LastName>singh</art:LastName>
</art:Name>
</art:Artist>

<art:Artist BirthYear="1942">
<art:Name>
<art:Title>Mr.</art:Title>
<art:FirstName>Mohit</art:FirstName>
<art:LastName>Sharma</art:LastName>
</art:Name>
</art:Artist>

</Artists>

</Song>

In this we have created an XML document that contains the Song element. This element uses two namespaces, http://www.namespace.com/Song and http://www.webucator.com/Artist.
The http://www.namespace.com/Song namespace is the default namespace while the http://www.webucator.com/Artist namespace is specified with the help of the art prefix. The default namespace is referred by the child elements, Title, Year, and Artists, of the Song element. However the child elements of the Artists element including ArtistBirthYear and Name refer to the art prefix.

The output of the multiplenamespaces.xml file.



How to use a default namespace in XML

That a default namespace does not have a prefix. Now create an XML document, named defaultnamespace.xml to learn how to use a default namespace.


/////defaultnamespace.xml file////
<?xml version="1.0"?>
<root>
<table xmlns="http://www.namespace.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
</root>

Here: root is the top most element that contains an instance of the table element. the table element uses the reference of a default namespace,
http://www.namespace.com/furniture, to avoid element name conflicts.

Shows the output of the defaultnamespace.xml file.



Thursday, 28 December 2017

How to working with XML Parsers in XML

An XML parser reads and updates an XML document by loading the document into the computers memory and then manipulate it using the DOM node-tree structure. It tests an XML document to check whether or not the document is well formed against the given DTD.  Now here we create an XML file employees.xml which is parsed by an XML parser.
<?XML version="1.0" encoding="ISO-8859-1"?>
<Employee>
<First_name>Ambrish</First_name>
<Middle_name>kumar </Middle_name>
<Last_name>singh</Last_name>
<Age>25</Age>
<Employee>
Next we use the SAX parser to check whether the XML document is well-formed or not. The SAX parser is available in the form of three jar files.
Like :
saxons.jar
saxon-jdom.jar
saxon-xml-apis.jar

Now we need to set the path of preceding JAR file in the classpath variable of environment variable in your system to run the SAX parser.

Here we have to create an HTML file xmlparser.html which uses SAX libraries to parse the employees.xml. That shows the code of the xmlparser.html file.

/////xmlparser.html file /////
<!doctype html>
<head>
<SCRIPT type="text/javascript">
var xmlParseDoc;
function loadParseXML()
{
if(window.ActiveXObject){
xmlParseDoc=new ActiveXObject("Microsoft.XMLDOM";
xmlParseDoc.async=false;
xmlParseDoc.load("employees.xml") ;
getmessage() ;
}
else if (document.implementation & & document.implementation.createDocument){
xmlParseDoc=document.implementation.createDocument("","",null);
xmlParseDoc.load("employees.xml") ;
xmlparser of.onload=getmessage;
}else {
alert('The script is not Compatible with your browser ') ;
}
} function getmessage()
{
document. getElementById("FirstName").innerHTML=xmlParseDoc.getElementByTagName("FirstName")[0].childNodes[0].nodeValue;
document.getElementById("MiddleName").innerHTML=
xmlParseDoc.getElementByTagName("MiddleName") [0].childNode[0].nideValue;
document. getElementById("LastName"). innerHTML=
xmlParseDoc.getElementsByTagName("LastName")[0].childNodes[0].nodeValue;
document.getElementById("Age").innerHTML=
xmlParseDoc.getElementsByTagName("Age")[0].childNodes[0].nodeValue;
}
</SCRIPT>
</head>
<body onload="loadParseXML()" >
<h1>Employee Details </h1>
<p><b>FirstName:</b>
<span id="FirstName"></span><br>
<b>Middle Name :</b><span id="MiddleName"></span><br>
<b>last Name :</b><span id="LastName"></span><br>
<b>Age:</b><span id="Age"></span>
</p>
</body>
</html>

We have used JavaScript to create two methods, loadParseXML()  and getMessage().
The loadParseXML() Method create an ActiveXObject object, xmlParseDoc, to load the XML document, employees.xml. Then, it calls the getMessage() method to retrieve the data stored in the XML document and display it on the browser. 

Tuesday, 26 December 2017

How to Create an XML Tree in XML

XML elements can be nested inside one another to make an XML Tree. Now create an XML document named xmltree.xml to understand how an XML document is represented as a tree.

///// xmltree.xml file /////

<person>
<name>
<first_name>petter </first_name>
<last_name>smith</last_name>
</name>
<address>
<house_no>26<house_no>
<lane_no>12</lane_no>
<city>Delhi </city>
<state>Delhi </state>
<country>india</country>
</address>
<contact_no>
<land_line>0000000000</land_line>
<mobile>1234567890</mobile>
</contact_no>
</person>

Here : person is the root element and name, address, and contact_no are it's child elements. The name element contains two child elements, first_name and last_name. The address element contains five child elements, house_no, lane_no, city, state, and country. The contact_no element contains two child elements land_line and mobile. 

How to Define an Attributes in XML

The XML elements can contain attributes to store data. Now here we understand how to Define an Attributes in XML creating an XML document name is xmlattributes.xml to learn how to define an Attributes in XML.

///// xmlattributes.xml file /////

<?xml version="1.0" encoding="UTF-8" standalone="yes"? >
<employees>
<firstname>jone</firstname>
<middlename>ni</middlename>
<lastname title="shinah"></lastname>
<birthday date="&lt;24/06/2001&gt;"></birthday>
<employees>

Here: The root element is employees, which contains four elements firstname, middlename, lastname, and birthday. The lastname and birthday elements have title and date attributes respectively. 

Sunday, 24 December 2017

Creating a Simple XML Document in XML

Here we create a simple XML document by using user-defined elements and attributes. Now create a valid XML document name is simplexmldoc.xml to understand the structure of an XML document.

///// simplexmldoc.xml /////
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<books>
<book>
<subject standard="class v">History </subject>
<author>D. P.Mishra </author>
<publisher>FGB</publisher>
<price>RS 200/-</price>
</book>

<book>
<subject standard="class v">Math </subject>
<author>D. S.Mishra </author>
<publisher>FGB</publisher>
<price>RS 270/-</price>
</book>

<book>
<subject standard="class v">English  </subject>
<author>R.S.KUMAR</author>
<publisher>FGB</publisher>
<price>RS 210/-</price>
</book>

<book>
<subject standard="class v">Science </subject>
<author>P.S SHARMA  </author>
<publisher>FGB</publisher>
<price>RS 250/-</price>
</book>
</books>

The XML declaration line version and encoding format of the XML document. Next the root element, books, contains the book element four times. The element further contains the subject, author, publisher, and price child elements.