Basics!
Q1: What is Xml ?
Data wrapped in tags.
Q2: How is it different from HTML ?
XML and HTML were designed with different goals:
- XML was designed to carry data - with focus on what data is
- HTML was designed to display data - with focus on how data looks
- XML tags are not predefined like HTML tags are
Q3: Why do we need them ?
- Data sharing
- Data transport
- Cross platform data exchange and interaction
Q4: What is prolog ?
A prolog defines the XML version and the character encoding:
<?xml version="1.0" encoding="UTF-8"?>
Q5: Why are the 7 rules of well-formed XML document ?
- Every starting and end tag
- Attributes values should be in quotes
- Entity References: Greater ,lesser etc signs must be specified in '<' this way ,parser won't understand them <message>salary < 1000</message>
- Proper nesting of elements
- Elements are case sensitive
- Doesn't truncate multiple white spaces
Q6: Element can contain
- text
- attributes
- other elements
- or a mix of the above
Q7: Attribute Rules
Specify in single or double quotes
Q8: Element vs Attribute
Some things to consider when using attributes are:
- attributes cannot contain multiple values (elements can)
- attributes cannot contain tree structures (elements can)
- attributes are not easily expandable (for future changes)
THUMB RULE: Choose attribute for atomic value
- Data sharing
- Data transport
- Cross platform data exchange and interaction
Q4: What is prolog ?
A prolog defines the XML version and the character encoding:
<?xml version="1.0" encoding="UTF-8"?>
Q5: Why are the 7 rules of well-formed XML document ?
- Every starting and end tag
- Attributes values should be in quotes
- Entity References: Greater ,lesser etc signs must be specified in '<' this way ,parser won't understand them <message>salary < 1000</message>
- Proper nesting of elements
- Elements are case sensitive
- Doesn't truncate multiple white spaces
Q6: Element can contain
- text
- attributes
- other elements
- or a mix of the above
Q7: Attribute Rules
Specify in single or double quotes
Q8: Element vs Attribute
Some things to consider when using attributes are:
- attributes cannot contain multiple values (elements can)
- attributes cannot contain tree structures (elements can)
- attributes are not easily expandable (for future changes)
THUMB RULE: Choose attribute for atomic value
Q8: What is xmlns attribute for ?
This XML carries HTML table information: TO avoid namespace conflicts you can specify
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
This XML carries information about a piece of furniture:
<table xmlns="https://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table xmlns="https://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
Q9: What is xmlns attribute for ?
Q9: Why is XMLHttpRequest object so important ?
The XMLHttpRequest object can be used to request data from a web server.
The XMLHttpRequest object is a developers dream, because you can:
- Update a web page without reloading the page
- Request data from a server - after the page has loaded
- Receive data from a server - after the page has loaded
- Send data to a server - in the background
Q10: What is DOM?What are various types?
The DOM defines a standard for accessing and manipulating documents:
HTML DOM
<h1 id="demo">This is a Heading</h1>
<button type="button"
onclick="document.getElementById('demo').innerHTML = 'Hello World!'">Click Me!
</button>
<button type="button"
onclick="document.getElementById('demo').innerHTML = 'Hello World!'">Click Me!
</button>
XML DOM
txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
Q11: Why is XPath ?
Navigate mechanism to access element either based on some attribute or criteria
- XPath is a syntax for defining parts of an XML document
- XPath uses path expressions to navigate in XML documents
- XPath contains a library of standard functions
- XPath is a major element in XSLT and in XQuery
Q12: Why is XSLT(Extensible stylesheet ?
Mechanism to transform XML to HTML
XSLT uses XPath to find information in an XML document.
Q12: Why is XQuery ?
XQuery is to XML what SQL is to databases.
XQuery was designed to query XML data.
Q14: Why is XLink?
XLink is used to create hyperlinks in XML documents.
<h xmlns:xlink="http://www.w3.org/1999/xlink">
<ho xlink:type="simple" xlink:href="https://www.w3schools.com">Visit W3Schools</hom>
<ho xlink:type="simple" xlink:href="http://www.w3.org">Visit W3C</ho>
</h>
<ho xlink:type="simple" xlink:href="https://www.w3schools.com">Visit W3Schools</hom>
<ho xlink:type="simple" xlink:href="http://www.w3.org">Visit W3C</ho>
</h>
xmlns:xlink
xlink:type
xlink:href
Q15: What is DTD?How many types of DTDs are there ?
There are two different document type definitions that can be used with XML:
- DTD - The original Document Type Definition
- XML Schema - An XML-based alternative to DTD
A document type definition defines the rules and the legal elements and attributes for an XML document.
Q16: What is DTD?How many types of DTDs are there ?
- xml DTD
- DTD
Q17: What is XML schema?
XML Schema is an XML-based alternative to DTD:
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Q17: What's the difference between ?
Q:What is JAXB needed for ?
For converting XML TO Java Object & vice versa
JAXB vs DOM and SAX
https://stackoverflow.com/questions/7709928/jaxb-vs-dom-and-sax
________________________________________________________
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tvidushi.JAXB; | |
import java.io.File; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Marshaller; | |
import javax.xml.bind.Unmarshaller; | |
/** | |
* | |
* @author takshila.vidushi | |
* | |
*/ | |
public class JAXBExample { | |
public static void main(String[] args) { | |
Person person = new Person(); | |
person.setId(1); | |
person.setName("Vidushi"); | |
objectToXML( person); | |
// xmlToObject(); | |
} | |
public static Person objectToXML(Person Person){ | |
try { | |
JAXBContext context = JAXBContext.newInstance(Person.class); | |
Marshaller marshal = context.createMarshaller(); | |
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE); | |
marshal.marshal(Person, System.out); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static Person xmlToObject(){ | |
Person emp = null ; | |
try { | |
JAXBContext context = JAXBContext.newInstance(Person.class); | |
Unmarshaller un = context.createUnmarshaller(); | |
emp = (Person)un.unmarshal(new File("person.xml")); | |
System.out.println(" id : "+emp.getId()); | |
System.out.println(" name : "+emp.getName()); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
return emp; | |
} | |
} |
Download complete example from
https://github.com/vidz2015/tommyhilfiger/tree/master/JAXB
No comments:
Post a Comment