Вы находитесь на странице: 1из 8

Vinod H Bachelor of Computer Application Semester - 5 BC0053 VB.

Net & XML


Fall 2012 Assignment Set 2 Roll No: 521110833 Center Code: 03011

2 1. What are syntax rules for writing XML file? Explain XML entities.

BC0053 VB.Net & XML Set -2

The syntax rules of XML are very simple and logical. The rules are easy to learn, and easy to use. a. XML consist of user-defined tags. Ex: <address> Hyderabad </Address> <state> AP</state> <city>HYD</city> b. We can describe the CML with the tags. c. In XML file each & every tag must have an ending tags Ex: <student> </student> To create the empty tags we use a single tag or a single line statement <student/> (i.e., <student></student>) d. XML is case sensitive Ex1: The below is the invalid XML file, because in the starting element (i.e. <student>) s is small and in the ending tag S is capital. <student> </Student> Ex2: The below is the valid XML <student></student> e. We cant create an XML file without a root element. Ex: In the below student.xml file <student> is the root element. <student> <sno> 1 </sno> <saddr> Hyderabad </saddr> </student> f. In XML we need to use only proper nesting.

Ex: The below is the valid XML file <student> <sno> 1 </sno> </student>

3 EX: The below is the invalid XML file, because of improper nesting. <student> <sno> 1 </student> </sno> g. In XML we use the comments as follows <! - - Comment - -!>

BC0053 VB.Net & XML Set -2

h. When we are declaring the attributes each and every attribute value must be enclosed with in the single quotes (or) double quotes. Ex: <student sno=1> </student> Or <student sno =1></student> i. Elements: An element consist of i. Simple Content: Consists of some text in between the tags. EX: <address> this is address </address> ii. Mixed Content: Consists of simple content and attributes. Ex: <address attr=some_value> this is address </address> iii. Complex Content: Consist of another tag Ex: <student> <sno attr=some_value> Content </sno> </student> Ex: <student> <smp> 1 </sno> <sname> aaa </sname> <marks> 23 </sname>

4 <address> bb </address> </student>

BC0053 VB.Net & XML Set -2

Entities are variables used to define shortcuts to standard text or special characters. Entity references are references to entities. Entities can be declared internal or external.

Some characters have a special meaning in XML, like the less sign (<) that defines the start of an XML tag. The following entities are predefined in XML: Entity References Character &lt; &gt; &amp; &quot; &apos; < > & " '

Entities can be declared either internal or external. Internal Entity declaration: This type of entities are declared in DTD document and accessed in the XML document. Here the content that the entity has to represent is specified in declaration part of the entity i.e. within the DTD document, thus these types of entities are known as internal entities. Syntax: <! ENTITY entity_name entity_value> Syntax to refer the entity: & entity_name; DTD Example: < ! ENTITY writer hai> <! ENTITY copyright copyright.yahoo>

5 Valid XML Example: <author> &writer; &copyright; </author> External Entity:

BC0053 VB.Net & XML Set -2

In this case the content that is to be referred by entity is placed into separated document instead of specifying into the same document. Syntax: <! ENTITY entity_name SYSTEM file path locating the document containing the content to refer> Example: <! ENTITY writer SYSTEM entities.dtd> XML Example: <author> &writer ;</author>

2. What are the methods and properties of XML DOM? Explain with examples. The XML Document Object Model (DOM) is a programming interface for XML documents. It defines the way an XML document can be accessed and manipulated. The XML DOM is a W3C Recommendation As a W3C specification, the objective for the XML DOM has been to provide a standard programming interface to a wide variety of applications. The XML DOM is designed to be used with any programming language and any operating system. With the XML DOM, a programmer can create an XML document, navigate its structure, and add, modify, or delete its elements. The process of taking a file and breaking it into its components is called parsing. The components are defined by a grammar, the rules of the language, although this may be implied by the file structure rather than formally specified. Parsing is one of the commonest activities carried out by software. There are four parameters which can be used to categorize parsers. They may be validating, non-validating, stream-based, or tree-based. A validating parser uses both an XML file and a DTD to check that the XML adheres to the rules of the application. If the XML breaks the rules by straying from the DTD then the parser will create an error and stop processing the files. Non-validating parsers are much more tolerant. They only use the XML document and are quite content if it is wee formed. A well-formed document is one which sticks to the general rules for XML such as having only one top-level element and no overlapping tags. The parser can operate using either a stream or a tree of data. Stream based parsers must read the entire document each time that an operation is requested and send a message to the controlling application when specific events occur. A tree based parser builds a static representation of the document which corresponds to the structure of the original XML. This tree may be updated by adding, removing, or modifying the nodes at run time. Two modules are commonly used for parsers: SAX and DOM. SAX parsers are used when dealing with stream of data. The SAX model is, though, unsuited to use on websites where repeated querying and updating of the XML document is required. In such cases a DOM based parser is the better. The DOM is a W3C (World Wide Web Consortium) standard.

BC0053 VB.Net & XML Set -2 The DOM defines a standard for accessing XML documents. The XML DOM is: o A standard object model for XML o A Standard programming interface for XML o Platform and Language-independent. o A W3C standard. The XML DOM defines the objects and properties of all XML elements, and the methods (interface) to access them. The XML DOM is a standard for how to get, change, add, or delete XML elements.

3. Explain XML Schema. What are the disadvantages of DTD? XML Schema includes constructors that help us to prepare XML markup language specifications, alternative to DTD. The XML Schema language is also referred to as XML Schema Definition (XSD). The disadvantages of DTD: While specifying the child elements in DTD it can only use /,* (or) + to describe the occurrence but in most of the cases we want to describe the exact number for the lower and upper limit. Like we may want the emps element to have minimum 5 emp elements and maximum 20. DTD does not have a support for common and simple types instead it provides only PCDATA. DTD does not support XML namespace. In case of DTD the element definition name and the element name is same. XML Schemas vs. DTDs: We think that very soon XML Schemas will be used in most Web applications as a replacement for DTDs. Here are some reasons: XML Schemas are extensible to future additions XML Schemas are richer and more powerful than DTDs XML Schemas are written in XML XML Schemas support data types XML Schemas support namespaces

4. What is Xpath? Explain Xpath string functions with examples? An XPath expression returns either a node-set, a string, a Boolean, or a number. String Functions Functions starts-with(string1, string2) contains(string1, string2) Description Returns true if the first string starts with the second string. Returns true if the first string contains the second string. Returns a section of the string. The section starts at offset (which is a substring(stringoffsetlength) number), and is as long as the value provided at length (also a number). substring-before(string1, string2) Returns the part of string1 up until the first occurence of string2.

7 substring-after(string1, string2) string-length(string)

BC0053 VB.Net & XML Set -2 Returns the part of string1 after the first occurence of string2. Returns the length of string (i.e. the number of characters). Trims the leading and trailing space from string. Also replaces normalize-space(string) consecutive occurrences of white space with a single space. Returns string1 after any matching characters in string2 have been translate(string1, string2, string3) replaced by the characters in string3. concat(string1, string2, ...) Concatenates all strings (i.e. joins them together). format-number(number1, string1, Returns a formatted string version of number1 after applying string1 string2) as a format string. string2 is an optional locale string.

5. What is DOM? Draw the DOM tree for emp.xml. The XML Document Object Model (DOM) is a programming interface for XML documents. It defines the way an XML document can be accessed and manipulated. The XML DOM is a W3C Recommendation As a W3C specification, the objective for the XML DOM has been to provide a standard programming interface to a wide variety of applications. The XML DOM is designed to be used with any programming language and any operating system. With the XML DOM, a programmer can create an XML document, navigate its structure, and add, modify, or delete its elements. The process of taking a file and breaking it into its components is called parsing. The components are defined by a grammar, the rules of the language, although this may be implied by the file structure rather than formally specified. Parsing is one of the commonest activities carried out by software. There are four parameters which can be used to categorize parsers. They may be validating, non-validating, stream-based, or tree-based. A validating parser uses both an XML file and a DTD to check that the XML adheres to the rules of the application. If the XML breaks the rules by straying from the DTD then the parser will create an error and stop processing the files. Non-validating parsers are much more tolerant. They only use the XML document and are quite content if it is wee formed. A well-formed document is one which sticks to the general rules for XML such as having only one top-level element and no overlapping tags. The parser can operate using either a stream or a tree of data. Stream based parsers must read the entire document each time that an operation is requested and send a message to the controlling application when specific events occur. A tree based parser builds a static representation of the document which corresponds to the structure of the original XML. This tree may be updated by adding, removing, or modifying the nodes at run time. Two modules are commonly used for parsers: SAX and DOM. SAX parsers are used when dealing with stream of data. The SAX model is, though, unsuited to use on Web sites where repeated querying and updating of the XML document is required. In such cases a DOM based parser is the better. The DOM is a W3C (World Wide Web Consortium) standard. The DOM defines a standard for accessing XML documents. The XML DOM is:

BC0053 VB.Net & XML Set -2 o A standard object model for XML o A standard programming interface for XML o Platform-and language-independent o A W3C standard The XML DOM defines the objects and properties of all XML elements, and the methods (interface) to access them. The XML DOM is a standard for how to get, change, add, or delete XML elements.

XML DOM Node Tree: In the DOM, everything in an XML document is a node. The DOM says: The entire document is a document node. Every XML element is an element node. The texts in the XML elements are text nodes. Every attribute is an attribute node. Comments are comment nodes.

The XML DOM views an XML document as a tree-structure. The tree structure is called a node-tree. All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created. The node tree shows the set of nodes, and the connections between them. The tree starts at the root node and branches out to the text nodes at the lowest level of the tree:

Вам также может понравиться