EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XML Tree
Secondary Sidebar
XML Tutorial
  • Basic
    • What is XML?
    • XML Tags
    • XML URL
    • XPath Sibling
    • XML root element
    • XML Encryption
    • XML Parsing php
    • xml parsing with java
    • Dataset XML
    • XML Parser in C#
    • XML Tree
    • XML boolean
    • XML sitemap
    • XML Array
    • XML reserved characters
    • XML Viewer
    • XML Uses
    • XML Special Characters
    • XML generator
    • XML file format
    • XML DOM
    • XML ampersand
    • XML Mapping
    • XML File
    • XML Element
    • XML HttpRequest
    • XML XSD
    • XML Schema
    • XML Namespaces
    • XML Comments
    • XML Attributes
    • XML Encoding
    • XML Validation
    • XML CDATA
    • XML Database
    • XML Technologies
    • XML Error
    • XML DTD
    • XML Date
    • XML Parsers
    • String in XML
    • XML with CSS
    • XML Versions
    • XML Features
    • XML Commands
    • Servlet web.xml
    • XPath Injection
    • XPath Functions
    • XPath starts-with
    • XPath Selector
    • XPath Count
    • XPath XML
    • XML Parsing in Oracle
    • XML parsing in python
  • Xpath
    • What is XPath?
    • XPath namespace
    • XPath for JSON
    • XPath Last Element
    • Xpath Substring
    • XPath First Element
    • XPath local-name
    • XPath Multiple Attributes
    • XPath Matches
    • XPath Position
    • XPath Index
    • XPath regex
    • XPath id contains
    • XPath innertext
    • XPath Multiple Conditions
    • XPath Helper
    • XPath Generator
    • XPath ID
    • XPath Locator

XML Tree

XML Tree

Introduction to XML Tree

The tree structure is also referred to as an XML tree model or hierarchical model. It can be defined as a hierarchical data structure with two additional benefits: list and property simplify working with an XML document by providing full authentic access whenever needed to all part of the document. This tree structure data model is the graphical representation of node elements to express their relationship with the tree pattern. Unlike other trees, XML doesn’t show the content instantly; only the structure has been shown. The tree structure has root elements followed by child elements and sub-child elements. To Pick the single nodes or a set of nodes, it is ultimate using Xpath.XML Tree structures gives their hand in implementation with python, C# languages.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The basic Syntax structure is drawn below:

XML-Tree-img1

Working of Tree

The tree is structured in a way similar to a tree connecting branches and leaves; the underlying tree structure forms an ancestor and descendant relationship. In this, the top element is placed at the root and the child elements are connected to the root element and certainly formed as element trees. As the tree has any number of child nodes (means any elements can have sub-elements). And again, the particular child nodes can behave as a root node, which can have any number of child nodes and so on until the depth. The tree structure enables the user to read the data in XML format. To understand the tree structure, it is necessary to read the basic XML terminologies and XML structure.

How to design a tree in XML?

At first, we shall see an XML document, and based on this; we can design a tree structure as the tree structure is the best supportive of XML language, which has the capability of deriving positions, powerfully combining hierarchical formats. We have some rules for designing a structure and a relationship between the two elements in XML Document. The rules are stated as follows:

Rule 1 – Descendants: If the XML element ‘X’ is contained by another element ‘Y’, X is Y’s descendant.

Rule 2 – Ancestors: If the XML element ‘X’ has element ‘Y’, then Y is the ancestors of X.

Rule 3 – The name of the label in the section data should be relative.

Examples of XML Document

Lets us first see the examples on the XML document

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,883 ratings)

Example #1

<?xml version="1.0" encoding="UTF-8"?>
<SuperMarket>
<product id="001">
<productname>fssai dates</productname>
<productmfd> 12 october</productmfd>
<productexpdate> 4 january</productexpdate>
<mfdaddress>
<city>bangalore</city>
<state>karnataka</state>
<pincode>201301</pincode>
<landmark>Near waterpark</landmark>
</mfdaddress>
</product>
</SuperMarket>

In this example, the first line states version of the XML document as “1.0”. So, here <supermarket> is the root element followed by many child elements.

XML Tree

The Tree structure for the above xml document is shown below:where root node is <supermarket>         , child element is <productname>,<product mfd>, < productexpdate> ,<mfdaddress> and the corresponding sub child element are <city>,<state>,<pincode>,<landmark>.

XML-Tree-img2

Computer Generated XML Tree in XML Viewer

When you click the down arrow, it elaborates to show the sub-child of the tree.

XML Tree output

Example #2

Implementation with attributes and values in the Xml document.

<?xml version="1.0" encoding="UTF-8"?>
<playstore app="game">
<title>Farmvillie- paris</title>
<developer>leonard britan</developer>
<CEO>mark kevin</CEO>
<released date="feb 23, 2001"></released>
<Music>
<Song no="1" duration="15:22">Wheels on the buse</Song>
<Song no="2" duration="16:25">Baby Shark</Song>
</Music>
</playstore>

In the above code, <playstore> is the root node and category type =” game” is a node attribute and year date=” Feb 23 2001” is a node value. Here both the game id and song no has enclosed text and known to be text nodes. Representing the above information in a tree format like:

chart-image

Result:

output.2

Example #3

Sample Implementation of the tree, which is parsed through Java.

Here we use several methods to create an element node, child node and text node and comment node like createElement(),appenrChild(),create TextNode() which is created in a text file.

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder =
builderFactory.newDocumentBuilder();
Document dt = docBuilder.newDocument();
new Main().createXmlTree(dt);
}
public void createXmlTree(Document dt) throws Exception {
Element root = dt.createElement("CBSESchool");
dt.appendChild(root);
Element child = dt.createElement("Accrediation");
root.appendChild(child);
Element child1 = dt.createElement("SchoolName");
child.appendChild(child1);
Text text = dt.createTextNode("St.Francis International School");
child1.appendChild(text);
Comment comment = dt.createComment("Faculties in the School");
child.appendChild(comment);
Element element = dt.createElement("Faculties");
child.appendChild(element);
Text text1 = dt.createTextNode("Nithya Ananda");
element.appendChild(text1);
Element chilE = dt.createElement("FID");
chilE.setAttribute("name", "Nithya");
root.appendChild(chilE);
// adding a text element to the child
Text text12 = dt.createTextNode("Qualification");
chilE.appendChild(text12);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter stw = new StringWriter();
StreamResult res = new StreamResult(stw);
DOMSource src = new DOMSource(dt);
transformer.transform(src, res);
String xmlString = stw.toString();
File file = new File("samplexml.xml");
BufferedWriter buf = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream(file)));
buf.write(xmlString);
buf.flush();
buf.close();
System.out.println("Generated XML file is:\n"
+ xmlString);
}
}

Result:

output.3

Conclusion

Therefore, we have gone through the tree, a family tree that provides easy access to manipulate the data within a tree in a flexible manner as it provides full access to the values in the XML document whenever it is needed. They form a huge foundation for a variety of data been used on the internet today. XML files are much handy to preserve program controls, and this tree provides great functionality to read the XML document. If the structure of the tree is constant, the complete XML tree is structured thoroughly.

Recommended Articles

This is a guide to XML Tree. Here we discuss How to design a tree in XML along with the working and examples on the XML document. You may also have a look at the following articles to learn more –

  1. XML Namespaces
  2. XML Comments
  3. XML DTD
  4. XML Versions
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more