Home >>XML Tutorial >XML Examples

XML Examples

XML Examples

XML documents build a hierarchical structure that looks like a circle, and it's known as XML Tree, which begins at "root" and branches to "leaves."

Example of XML Document

XML documents use the basic and self-descriptive syntax:


<?xml version="1.0" encoding="ISO-8859-1"?>  
<note>  
  <to>Noida</to>  
  <from>Delhi</from>  
  <heading>Place</heading>  
  <body>Hello Phptpoint</body>  
</note> 

The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/West European character set).

The next line describes the document's root element (like saying: "this document is a note"):

<note>

The following 4 lines describe 4 root elements (to, from, heading, and body) of the child.

<to>Noida</to>

<from>Delhi</from>

<heading>Place</heading>

<body>Hello Phptpoint</body>

And finally the last line defines the end of the root element.

</note>

XML documents have to have a root element in them. This part is the "father" of all other components.

The items in an XML document make up a tree for a file. The tree starts at the base, and branches to the tree's lowest level.

All elements may have sub-elements (elements for child).

<root>  
  <child>  
    <subchild>.....</subchild>  
  </child>  
</root>  

The words parent, child, and sibling are used to define the cross-element relationships. Elements of the parents have children. On the same level children are called siblings (brothers or sisters).

Both components are worthy of providing text information and attributes (as in HTML).

Another Example of XML: Books

File: books.xml


<bookstore>  
  <book category="PROGRAMMING">  
    <title lang="en">Every Course</title>  
    <author>Phptpoint</author>  
    <year>2020</year>  
    <price>70.00</price>  
  </book>  
  <book category="HTML">  
    <title lang="en">Learn Html</title>  
    <author>Phptpoint</author>  
    <year>2020</year>  
    <price>45.99</price>  
  </book>  
  <book category="WEB">  
    <title lang="en">Learning XML</title>  
    <author>Phptpoint</author>  
    <year>2029</year>  
    <price>75</price>  
  </book>  
</bookstore>

The root element in the example is <bookstore>. All elements in the document are contained within <bookstore>.

The <book> element has 4 children: <title>,< author>, <year> and <price>.

Another Example of XML: Emails

File: emails.xml

 
 <?xml version="1.0" encoding="UTF-8"?>  
<emails>  
<email>  
  <to>A</to>  
  <from>B</from>  
  <heading>Hi</heading>  
  <body>Hi phptpoint!</body>  
</email>  
<email>  
  <to>C</to>  
  <from>D</from>  
  <heading>hello</heading>  
  <body>Hello phptpoint!</body>  
</email>  
<email>  
  <to>E</to>  
  <from>F</from>  
  <heading> hello</heading>  
  <body>Hello World!</body>  
</email>  
<email>  
  <to>G</to>  
  <from>H</from>  
  <heading>Skills</heading>  
  <body>Programming skills!</body>  
</email>  
</emails>  
 
 

No Sidebar ads