Introduction to Web Services Development (CS311) - Updated Handouts.pdf

Document Details

SweetheartLutetium

Uploaded by SweetheartLutetium

Virtual University of Pakistan

Tags

web services java programming computer science

Full Transcript

Introduction to Web Services Development (CS311) Introduction to Web Services Development CS311 – Handouts Syed Nauman Ali Shah © Copyright Virtual University of Pakistan 1 Introduction to Web Services Development...

Introduction to Web Services Development (CS311) Introduction to Web Services Development CS311 – Handouts Syed Nauman Ali Shah © Copyright Virtual University of Pakistan 1 Introduction to Web Services Development (CS311) Contents Booklet of Introduction to Web Services Development..................................................................... 1 Contents................................................................................................................................................ 2 Module 1 (Object Oriented Paradigm)................................................................................................ 3 Module 2: Basics of Java Programming.............................................................................................38 Module 3: (Program Control Flow)....................................................................................................46 Module 4: (Using Objects)..................................................................................................................56 Module 5: (Primitive values as objects).............................................................................................71 Module 6: (Arrays)..............................................................................................................................76 Module 7: (Classes).............................................................................................................................82 Module 8 (Object Communication).................................................................................................118 Module 9: (Modifiers).......................................................................................................................129 Module 10: (Exception Handling).....................................................................................................143 Module 11: (XML).............................................................................................................................153 Module 12: (XML).............................................................................................................................175 Module 13 :( XML DOM)...................................................................................................................197 Modue 14: Introduction to JAXP......................................................................................................246 Module 15: (Servlets)........................................................................................................................263 Module-16: Java Database Connectivity (JDBC)..............................................................................327 Module 17 Introduction WebServices.............................................................................................338 Module 18 Web Services Architecture............................................................................................345 Module 19 Web Services with JAX-WS............................................................................................353 © Copyright Virtual University of Pakistan 2 Introduction to Web Services Development (CS311) Module 1 (Object Oriented Paradigm) Objective: In this lecture we will discuss Object Orientation in detail. It will be followed by discussion on Data Modeling with reference to Abstraction, Inheritance, Encapsulation and Information Hiding, what are the advantages and they are used in classes and their examples. What is an Object Orientation? In real world, object oriented approach focuses on objects that represents abstract or concrete things. What is a Model? Modeling is an approach that is used at the beginning of software. The reasons to model a system are: Communication: Model diagrams before implementation can be more understandable and can allow users to give developers feedback on the appropriate structure of the system. Abstraction: The goal of the software methodology is to first what functionality is used and then how to take this abstract description and refine it into an implementable design. What is an Object? For understanding an object orientation, you need to understand object first. Objects are key to understand object-oriented. Look around yourself and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. Object is something tangible. Object has two characteristics; state and behavior. State is a noun, attribute, a well-defined behavior and behavior is a certain operation. For example dog which is a tangible object, an object which can be touched has a state of colour, name, and breed and has behavior of barking, fetching. © Copyright Virtual University of Pakistan 3 Introduction to Web Services Development (CS311) Object "Object" can be a combination of variables and functions. You may also notice that some objects, in turn, will also contain other objects. Relationships of identified objects are constructed (such as relating a person's age to a specific person). For example there are several objects like Ali, House, Car, and Tree. Relationships are built between these objects like; Ali lives in the house and Ali drives a car. In these two sentences, Ali, house and car are objects which interact with each other and form a certain relation. Time is another example in which Hours, minutes and seconds are the objects and the operations performed on time like set Hours, set minutes and set seconds are the behavior and operations performed on the object. Advantages: In real world, we are surrounded by objects. Modeling picks up each thing/object in the real world which is involved in the requirement. It makes the requirement simple and easily understandable by representing simple diagrams. What is Abstraction? Abstraction is the process of taking out characteristics from something in order to reduce it into a set of essential characteristics. Through the process of abstraction, a programmer hides all the irrelevant data about an object in order to reduce complexity and increase efficiency. © Copyright Virtual University of Pakistan 4 Introduction to Web Services Development (CS311) Examples: Ali is a PhD student and teaches BS students. Below table shows the attributes of Ali as a student and employee. When Ali is at university, then he is a "Student". When he is at work, he is an "Employee". Attributes: Name Employee ID Student Roll No Designation Year of Study Salary CGPA Age Ali has different relationships in different roles. So it boils down to what in what context we are looking at an entity/object. So if I am modelling a Payroll System, I will look at Ali as an Employee (employee ID, name, designation, salary and age) shown in the table below. Attributes: Employee ID Name Designation Salary Age If am modelling a Course Enrollment System, then I will consider Ali’s aspects and characteristics as a Student (student roll number, name, year of study, cgpa, age) shown in the table below. Attributes: Name Student Roll No Year of Study CGPA Age © Copyright Virtual University of Pakistan 5 Introduction to Web Services Development (CS311) Abstract class and abstract method. Abstraction means putting all the variables and methods in a class which are necessary. Abstraction is the common thing. Example: If somebody in your collage tell you to fill application form, you will fill your details like name, address, data of birth, which semester, percentage you have got etc. If some doctor gives you an application to fill the details, you will fill the details like name, address, date of birth, blood group, height and weight. See in the above example what is the common thing? Age, name, address so you can create the class which consist of common thing that is called abstract class. That class is not complete and it can inherit by other class. Graphical Representation of Classes The class is represented as a rectangle, divided in 3 boxes one under another. The name of the class is at the top. Next, there are the attributes of the class. At the very bottom are the operations or methods. The plus/minus signs indicate whether an attribute / operation is visible (+ means public) or not visible (- means private). Protected members are marked with #. (Class Name) -(Attribute) (Class Name) +(Operations) Suppressed Form Normal Form Abstraction in Programming: © Copyright Virtual University of Pakistan 6 Introduction to Web Services Development (CS311) A Car has Engine, wheels and many other parts. When we write all the properties of the Car, Engine, and wheel in a single class, it would look this way: public class Car { int price; String name; String color; int engineCapacity; int engineHorsePower; String wheelName; int wheelPrice; void move(){ //move forward } void rotate(){ //Wheels method } void internalCombustion(){ //Engine Method } } In the above example, the attributes of wheel and engine are added to the Car type. This will not create any kind of issues programming. But it becomes more complex when it comes to maintenance of the application. Abstraction has three advantages:  By using abstraction, we can separate the things that can be grouped to another type.  Frequently changing properties and methods can be grouped to a separate type so that the main type need not undergo changes.  Simplifies the representation of the domain models. © Copyright Virtual University of Pakistan 7 Introduction to Web Services Development (CS311) Applying the abstraction with composition, the above example can be modified as given below: public class Car { Engine engine = new En gine(); Wheel wheel = new Whee l(); int price; String name; String color; void move(){ //move forward } } public class Engine { int engineCapacity; int engineHorsePower; void internalCombustion( ){ //Engine Method } } public class Wheel { String wheelName; int wheelPrice; void rotate(){ //Wheels method } } © Copyright Virtual University of Pakistan 8 Introduction to Web Services Development (CS311) Engine and Wheel are referred from the Car type. Whenever an instance of Car is created, both Engine and Wheel will be available for the Car and when there are changes to these Types (Engine and Wheel), changes will only be confined to these classes and will not affect the Car class. What is Inheritance? Inheritance enables new objects to take on the properties of existing objects. A class that is used as the basis for inheritance is called a superclass or base class. A class that inherits from a superclass is called a subclass or derived class. The terms parent class and child class are also acceptable terms to use respectively. The parent class is called base class and the child class is called derived class. A child inherits characteristics from its parent while adding additional characteristics of its own. Subclasses and superclasses can be understood in terms of the is a relationship. A subclass is a more specific instance of a superclass. For example, orange is a fruit, parrot is a bird. An orange is a fruit; so it is okay to write an Orange class is a subclass of a Fruit class. Examples: If a class B inherits from class A then it contains all the characteristics (information structure and behaviour) of class A as shown in figure below. © Copyright Virtual University of Pakistan 9 Introduction to Web Services Development (CS311) Super Class A "is a" B Sub Class Person Student Teacher Doctor In the example above, Class Student, Teacher and Doctor inherits from Class Person. Each child class contain “is a” relation with parent class. Student “is a” person, Teacher “is a” person and Doctor “is a” person. Each child contains all the characteristics of parent class. Similarly: Shape Line Circle Triangle © Copyright Virtual University of Pakistan 10 Introduction to Web Services Development (CS311) In the example above, Class Line, Circle and Triangle inherits from Class Shape. Each child class contain “is a” relation with parent class. Line “is a” Shape, Circle “is a” shape and Triangle “is a” Shape. Each child contains all the characteristics of parent class. Generalization: Generalization is the process of extracting shared characteristics from two or more classes, and combining them into a generalized superclass. Shared characteristics can be attributes, associations, or methods. The classes Circle and Rectangle partially share the same attributes. From a domain perspective, the two classes are also very similar. During generalization, the shared characteristics are combined and used to create a new superclass Shape. Circle and Rectangle become subclasses of the class Shape. The shared attributes are only listed in the superclass, but also apply to the two subclasses, even though they are not listed there. © Copyright Virtual University of Pakistan 11 Introduction to Web Services Development (CS311) Example of Generalization Specialization: In contrast to generalization, specialization means creating new subclasses from an existing class. If it turns out that certain attributes, associations, or methods only apply to some of the objects of the class, a subclass can be created. The most inclusive class in a generalization/specialization is called the superclass and is generally located at the top of the diagram. The more specific classes are called subclasses and are generally placed below the superclass. The class Freight has the attribute Degree of Hazardousness, which is needed only for cargo, but not for passenger luggage. Obviously, here two similar but different domain concepts are combined into one class. Through specialization the two special cases of freights are formed: Piece of Cargo and Piece of Luggage. The attribute Degree of Hazardousness is placed where it belongs—in Piece of Cargo. The attributes of the class Freight also apply to the two subclasses Piece of Cargo and Piece of Luggage: © Copyright Virtual University of Pakistan 12 Introduction to Web Services Development (CS311) Example of specialization Specialization & Extension: As the name suggests, class extension is concerned with adding something to a class. We can add both variables and operations. These considerations lead us to the following definition of class extension. If class B is an extension of class A then  B may add new variables and operations to A  Operations and variables in A are also present in B  B-objects are not necessarily conceptually related to A-objects © Copyright Virtual University of Pakistan 13 Introduction to Web Services Development (CS311) Here are the three classes given below that specialize the class BankAccount. A specialization hierarchy of bank accounts Figure below shows the extensions of the bank account classes. The specialized bank accounts overlap in such a way that there can exist a bank account which is both a CheckAccount, a SavingsAccount, and a LotteryAccount. An overlapping is the prerequisite for multiple specialization. Possible extensions of the bank account classes Specialization & Restriction: Specialization means that derived class is behaviorally incompatible with the base class, behaviorally incompatible means that base class can’t always be replaced by the derived class. For example, new subclasses from an existing class. Through specialization, Person class is formed: Age is an attribute which is common for every person but in subclass, a condition is defined in which age of an adult should be above 18 otherwise, error will occur. So the subclass was restricted to adult age which should be above 18. © Copyright Virtual University of Pakistan 14 Introduction to Web Services Development (CS311) Advantages: Main purpose of inheritance is reuse: We can easily add new classes by inheriting from existing classes and can select an existing class closer to the desired functionality. We can create a new class and inherit it from the selected class. We can add to and/or modify the inherited functionality. The classes Student and Teacher partially share the same attributes. From a domain perspective, the two classes are also very similar. During generalization, the shared characteristics are combined and used to create a new superclass Person. Student and Teacher become subclasses of the class Person. The shared attributes are only listed in the superclass like name, age and gender, but also apply to the two subclasses, even though they are not listed there. A new class of doctor has common attributes of class Teacher so the teacher class eventually become the superclass of Doctor. As Teacher is a subclass of Person class so the Doctor class will indirectly have the properties of Person class as well as shown in figure below. © Copyright Virtual University of Pakistan 15 Introduction to Web Services Development (CS311) If the inheritance of Doctor Class is changed from Teacher class to Person class, there will be no change in Teacher class. Doctor class will still be the same but only the functions of Teacher class will be excluded. © Copyright Virtual University of Pakistan 16 Introduction to Web Services Development (CS311) What is Encapsulation? An object has to provide its users only with the essential information for manipulation, without the internal details. A Secretary using a Laptop only knows about its screen, keyboard and mouse. Everything else is hidden internally under the cover. She does not know about the inner workings of Laptop, because she doesn’t need to. Therefore parts of the properties and methods remain hidden to her. The person writing the class has to decide what should be hidden and what not. When we program, we must define as private every method or field which other classes should not be able to access. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding (not data security). Thus encapsulation is said to be providing “access control” through which we can control which parts of the program can access the members of any class and thus prevent misuse. Various access controls are public, private and protected. Real world Example of Encapsulation:- Let's take example of Mobile Phone and Mobile Phone Manufacturer Suppose you are a Mobile Phone Manufacturer and you designed and developed a Mobile Phone design(class), now by using machinery you are manufacturing a Mobile Phone(object) for selling, when you sell your Mobile Phone the user only learn how to use the Mobile Phone but not that how this Mobile Phone works. Another example is the TV operation. It is encapsulated with cover and we can operate with remote and no need to open TV and change the channel. Here everything is in private except remote so that anyone can access not to operate and change the things in TV. © Copyright Virtual University of Pakistan 17 Introduction to Web Services Development (CS311) Implementation: Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public. So, when you access the property you can validate the data and set it. Example: class Demo { private int _mark; public int Mark { get { return _mark; } set { if (_mark > 0) _mark = value; else _mark = 0; } } } Advantages: The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code. What is Information Hiding? Information hiding concept restricts direct exposure of data. All information should not be accessible to all persons. Private information should only be accessible to its owner. By Information Hiding we mean “Showing only those details to the outside world which are necessary for the outside world and hiding all other details from the outside world.” © Copyright Virtual University of Pakistan 18 Introduction to Web Services Development (CS311) Your name and other personal information is stored in your brain we can’t access this information directly. For getting this information we need to ask you about it and it will be up to you how much details you would like to share with us. An email server may have account information of millions of people but it will share only our account information with us if we request it to send anyone else accounts information our request will be refused. A phone SIM card may store several phone numbers but we can’t read the numbers directly from the SIM card rather phone-set reads this information for us and if the owner of this phone has not allowed others to see the numbers saved in this phone we will not be able to see those phone numbers using phone. Advantages of Information Hiding  Information Hiding makes easier for everyone to understand object oriented model.  It is a barrier against change propagation. As implementation of functions is limited to our class and we have only given the name of functions to user along with description of parameters so if we change implementation of function it doesn’t affect the object oriented model. © Copyright Virtual University of Pakistan 19 Introduction to Web Services Development (CS311) Types of Inheritance: Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class. The below flow diagram shows that class Circle extends only one class which is Shape. Here Shape is a parent class of Circle and Circle would be a child class of Shape. Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes. In Multilevel inheritance there is a concept of grandparent class. As you can see in below flow diagram Semicircle is subclass or child class of Circle and Circle is a child class of Shape. So in this case class Semicircle is implicitly inheriting the properties and method of class Shape along with Circle that’s what is called multilevel inheritance. Multiple Inheritance: © Copyright Virtual University of Pakistan 20 Introduction to Web Services Development (CS311) It is the inheritance hierarchy wherein one derived class inherits from multiple base class (es). The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes. In figure below, Mermaid is child class of class Woman and Fish. Mermaid will have all the properties of both Woman and Fish class. There are problems in multiple inheritance. Both woman and Fish have the attribute of eat with their aspects so duplicate attribute of eat in Mermaid will cause complexity in choosing which property will Mermaid will follow as shown in figure below. Links & Relationship: © Copyright Virtual University of Pakistan 21 Introduction to Web Services Development (CS311) Association Class Association Object Association Inheritance Simple Association Composition Aggregation Simple Association Association is a relationship between two objects. In other words, Two objects may depend on each other but don’t interact directly (weak association). Association defines the multiplicity between objects. For example, the project management system involves various general relationships, including manage, lead, execute, input, and output between projects, managers, teams, work products, requirements, and systems. Consider, for example, how a project manager leads a team. One Way Association: Associations are generally assumed to be bi-directional i.e. a message can pass in both directions between objects. However in implementation this doesn't have to be the Case as shown in the example bellow: Single directional arrow shows the message conveying from one object to other. For example: Ali lies in house but house lives in Ali will be incorrect. Moreover, Remote operates TV and not TV operates Remote so these both are one way Associations. Ali Lives House Remote Operates TV and Two Way Association: © Copyright Virtual University of Pakistan 22 Introduction to Web Services Development (CS311) This type of association is bi-directional i.e. a message can pass in both directions between objects. It is usually donated by a single straight line or an arrow on both sides of the objects. For example, Employee works for company and the company employs many employees. Ternary Association Any association may be drawn as a diamond with a solid line for exactly 3 associations end connecting the diamond. For example, Teacher can teaches many courses, Many Students can enroll many courses, each teacher can teaches many students. N-ary association with more than two ends can only be drawn this way. Composition: Composition is specialized form of Aggregation. It is a strong type of Aggregation. Child object does not have its lifecycle and if parent object is deleted, all child objects will also be deleted. Let’s take again an example of relationship between House and Rooms. House can contain multiple rooms - there is no independent life of room and any room cannot belong to two different houses. If we delete the house - room will automatically be deleted. Let’s take another example relationship between Questions and Options. Single questions can have multiple options © Copyright Virtual University of Pakistan 23 Introduction to Web Services Development (CS311) and option cannot belong to multiple questions. If we delete questions options will automatically be deleted. This is a strong type of relation because composed objects become the part of the composer. For example, Ali is made of 1 Head, 2 Arm, 2 Leg and 1 Body. These four classes cannot exist independently. If Ali is deleted, all four classes will automatically be removed because these classes are dependent on Ali. Aggregation: Aggregation is a specialized form of Association where all objects have their own lifecycle, but there is ownership and child objects can not belong to another parent object. Take an example of Department and teacher. A single teacher cannot belong to multiple departments, but if we delete the department teacher object will not be destroyed. We can think about it as a “has-a” relationship. Aggregation is weaker relationship, because aggregate object is not a part of the container, they can exist independently. Aggregation is represented by a line with unfilled- diamond head towards the container. Direction between them specified which object contains the other object. In the example below, Room has 1 bed, 1 table, 1 cupboard and many chairs. Furniture is not the intrinsic part of the composer. They are weak aggregation and can exist independently. Furniture can shift to other room so they can exist independent of a particular room. © Copyright Virtual University of Pakistan 24 Introduction to Web Services Development (CS311) Take another example of plant and garden. They are weak aggregation and can exist independently. Plant is not the intrinsic part of the garden and can exist independently. They can be planted to other garden and it is not dependent to the particular garden. Abstract Class: An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. An abstract method is a method that is declared without an implementation. If a class includes abstract methods, then the class itself must be declared abstract. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract. For example, you can draw circles, rectangles, triangles and many other shapes. These objects all have certain states (for example: center, orientation, line color, fill color) and behaviors (for example: area, rotate, draw) in common. Some of these states and behaviors are the same for all shapes (for example: center, area, and draw). All shapes must be able to draw themselves; they © Copyright Virtual University of Pakistan 25 Introduction to Web Services Development (CS311) just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object (for example, Shape) as shown in the following figure. Classes Rectangle, triangle and Circle Inherit from Shape. Concrete Class: A concrete class has concrete methods, i.e., with code and other functionality. This class may extend an abstract class or implements an interface. The derived class is expected to provide implementations for the methods that are not implemented in the base class. A derived class that implements all the missing functionality is called a concrete class. In example below, Student, Teacher and Doctor are concrete classes and Person is the Abstract class. © Copyright Virtual University of Pakistan 26 Introduction to Web Services Development (CS311) Interface (Java): An interface is an elegant way of defining the public services that a class will provide without being bogged down by implementation details. An interface is used to define the public services of a class. The interface provides no implementation details. Think of an interface as a business contract between two parties. The class implementing the interface agrees to provide the services defined in that interface to other classes. The other classes calling on the public services agree to abide by the semantics of the interface. In the diagram below, both the Professor and Student classes implement the Person interface and do not inherit from it. We know this for two reasons: 1) The Person object is defined as an interface — it has the "«interface»" text in the object's name area, and we see that the Professor and Student objects are class objects because they are labeled according to the rules for drawing a class object. 2) We know inheritance is not being shown here, because the line with the arrow is dotted and not solid. As shown in Figure, a dotted line with a closed, unfilled arrow means realization (or implementation); a solid arrow line with a closed, unfilled arrow means inheritance. © Copyright Virtual University of Pakistan 27 Introduction to Web Services Development (CS311) Overloading: Overloading occurs when two or more methods in one class have the same method name but different parameters. An appropriate example would be a Print(object O) method. In this case one might like the method to be different when printing, for example, text or pictures. The two different methods may be overloaded as Print(text_object T); Print(image_object P). If we write the overloaded print methods for all objects our program will "print", we should not have to worry about the type of the object, and the correct function call again, the call is always: Print(something). Rules: The overloaded function must differ by data types... The same function name is used for various instances of function call. © Copyright Virtual University of Pakistan 28 Introduction to Web Services Development (CS311) Programming Example: In the example below variables are added with two datatypes, int and double. Through datatype int, out will be in real numbers and through double, the output will be in floating numbers as shown below. The method is same of addition in both case but from different datatypes, the output is different. #include #include void add(int x, int y); void add(double x, double y); int main() { clrscr(); add(10,20); add(10.4,20.4); return(0); } void add(int x, int y) { cout } void add(double x,double y) { cout } Run output 30 30.8 © Copyright Virtual University of Pakistan 29 Introduction to Web Services Development (CS311) Overriding: Overriding means having two methods with the same method name and parameter. One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class. One of the simplest example – Here Boy class extends Human class. Both the classes have a common method void eat(). Boy class is giving its own implementation to the eat() method or in other words it is overriding the method eat(). class Human{ public void eat() { System.out.println("Human is eating"); } } class Boy extends Human{ public void eat(){ System.out.println("Boy is eating"); } public static void main( String args[]) { Boy obj = new Boy(); obj.eat(); } } Output: Boy is eating. © Copyright Virtual University of Pakistan 30 Introduction to Web Services Development (CS311) Here is another example of overriding. The dog variable is declared to be a Dog. During compile time, the compiler checks if the Dog class has the bark() method. As long as the Dog class has the bark() method, the code compilers. At run-time, a Hound is created and assigned to dog. Dog is referring to the object of Hound, so it calls the bark() method of Hound. class Dog{ public void bark(){ System.out.println("woof "); } } class Hound extends Dog{ public void sniff(){ System.out.println("sniff "); } public void bark(){ System.out.println("bowl"); } } public class OverridingTest{ public static void main(String [] args){ Dog dog = new Hound(); dog.bark(); } } Output: Bowl The main advantage of overriding is that the class can give its own specific implementation to an inherited method without even modifying the parent class(base class). © Copyright Virtual University of Pakistan 31 Introduction to Web Services Development (CS311) Polymorphism: "Poly" means "many" and "morph" means "form". Polymorphism is an object-oriented programming concept that refers to the ability of a variable, function or object to take on multiple forms. There are two types of polymorphism one is compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is functions and operators overloading. Runtime time polymorphism is done using inheritance and virtual functions. Here are some ways how we implement polymorphism in Object Oriented programming languages. Compile time polymorphism -> Operator Overloading, Function Overloading Run time polymorphism -> Interface and abstract methods, Virtual member functions. An example would be: A "Shape" class can be a part of an inheritance hierarchy where derived classes are "Circle", “Triangle” and "Rectangle". Derived from "Rectangle" could be "Square", Now, using such an example, it is true that any object below in a hierarchy is also something that is directly up in the hierarchy. Hence, Square “is a" Rectangle, and Rectangle "is a" Shape. Also, Square "is a" Shape. Each of these classes will have different underlying data. A point shape needs only two co- ordinates (assuming it's in a two-dimensional space of course). A circle needs a center and © Copyright Virtual University of Pakistan 32 Introduction to Web Services Development (CS311) radius. A square or rectangle needs two co-ordinates for the top left and bottom right corners (and possibly) a rotation. And, by making the class responsible for its code as well as its data, you can achieve polymorphism. In this example, every class would have its own Draw() function and the client code could simply do: shape.Draw(). Hence, using pointers of Base classes (higher in an inheritance hierarchy) can be assigned to objects of derived classes and can be used in a unified manner with the use of virtual functions. Hence, Polymorphism. (The plus "+" operator example used above would not be correct, as that is actually an overloaded operator (in the case the last poster presumed) and not precisely polymorphism). Dynamic Binding: Late binding means the binding occurs at runtime, based on the type of the object. Late binding is also called dynamic binding or runtime binding. When a language implements late binding, there must be some mechanism to determine the type of the object at runtime and call the appropriate member function. In the case of a compiled language, the compiler doesn’t know the actual object type, but it inserts code that finds out and calls the correct function body. An example of polymorphism is the technique by which a reference that is used to invoke a method can actually invoke different methods at different times depending on what it is referring to at the time. This can be illustrated by the following example. In the for loop, the statement all[i].toString() will either invoke the definition in Student or MScStudent depending on what type of object the polymorphic reference all[i] is pointing to at the time. © Copyright Virtual University of Pakistan 33 Introduction to Web Services Development (CS311) public class TestPoly2 { public static void main(String [] args) { Student [] all= new Student; all= new Student("kate"); all= new MScStudent("mike"); all= new Student("Jane"); for (int i=0;i ]> Tove Jani Reminder Don't forget me this weekend! The DTD is interpreted like this: !ELEMENT note (in line 2) defines the element "note" as having four elements: "to,from,heading,body". !ELEMENT to (in line 3) defines the "to" element to be of the type "CDATA". !ELEMENT from (in line 4) defines the "from" element to be of the type "CDATA" and so on..... © Copyright Virtual University of Pakistan 159 Introduction to Web Services Development (CS311) External DTD This is the same XML document with an external DTD: Tove Jani Reminder Don't forget me this weekend! This is a copy of the file "note.dtd" containing the Document Type Definition: Why use a DTD? XML provides an application independent way of sharing data. With a DTD, independent groups of people can agree to use a common DTD for interchanging data. Your application can use a standard DTD to verify that data that you receive from the outside world is valid. You can also use a DTD to verify your own data. A lot of forums are emerging to define standard DTDs for almost everything in the areas of data exchange. © Copyright Virtual University of Pakistan 160 Introduction to Web Services Development (CS311) Unicode Characters: The table below lists the five XML predefined entities. The "Name" column mentions the entity's name. The "Character" column shows the character. To render the character, the format &name; is used; for example, &amp; renders as &. The "Unicode code point" column cites the character with standard UCS/Unicode "U+" notation, which shows the character's code point in hexadecimal. The decimal equivalent of the code point is then shown in parentheses. The "Standard" column indicates the first version of XML that includes the entity. The "Description" column cites the character with its UCS/Unicode name. Name Character Unicode code point (decimal) Standard Description quot " U+0022 (34) XML 1.0 double quotation mark amp & U+0026 (38) XML 1.0 ampersand apos ' U+0027 (39) XML 1.0 apostrophe (apostrophe-quote) lt < U+003C (60) XML 1.0 less-than sign gt > U+003E (62) XML 1.0 greater-than sign XML Namespaces XML Namespaces provide a method to avoid element name conflicts. Name Conflicts In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications. This XML carries HTML table information: Apples Bananas © Copyright Virtual University of Pakistan 161 Introduction to Web Services Development (CS311) This XML carries information about a table (a piece of furniture): African Coffee Table 80 120 If these XML fragments were added together, there would be a name conflict. Both contain a element, but the elements have different content and meaning. A user or an XML application will not know how to handle these differences. Solving the Name Conflict Using a Prefix Name conflicts in XML can easily be avoided using a name prefix. This XML carries information about an HTML table, and a piece of furniture: Apples Bananas African Coffee Table 80 120 In the example above, there will be no conflict because the two elements have different names. © Copyright Virtual University of Pakistan 162 Introduction to Web Services Development (CS311) XML usage: XML allows sets of documents which are all the same type to be created and handled consistently and without structural errors, because it provides a standardized way of describing, controlling, or allowing/disallowing particular types of document structure. XML provides a common syntax for messaging systems for the exchange of information between applications. Previously, each messaging system had its own format and all were different, which made inter-system messaging unnecessarily messy, complex, and expensive. If everyone uses the same syntax it makes writing these systems much faster and more reliable. XML is free. It doesn't belong to anyone, so it can't be hijacked or pirated. And you don't have to pay a fee to use it. XML information can be manipulated programmatically so XML documents can be pieced together from disparate sources, or taken apart and re-used in different ways. They can be converted into any other format with no loss of information. XML can also be used to store data in files or in databases. Applications can be written to store and retrieve information from the store, and generic applications can be used to display the data. XML is Often a Complement to HTML In many HTML applications, XML is used to store or transport data, while HTML is used to format and display the same data. XML Separates Data from HTML When displaying data in HTML, you should not have to edit the HTML file when the data changes. With XML, the data can be stored in separate XML files. © Copyright Virtual University of Pakistan 163 Introduction to Web Services Development (CS311) With a few lines of JavaScript code, you can read an XML file and update the data content of any HTML page. XML Tree Structure XML documents form a tree structure that starts at "the root" and branches to "the leaves". XML documents are formed as element trees. An XML tree starts at a root element and branches from the root to child elements. All elements can have sub elements (child elements):..... The terms parent, child, and sibling are used to describe the relationships between elements. © Copyright Virtual University of Pakistan 164 Introduction to Web Services Development (CS311) Parent have children. Children have parents. Siblings are children on the same level (brothers and sisters). XML Element An XML element is everything from (including) the element's start tag to (including) the element's end tag. 29.99 An element can contain:  text  attributes  other elements  or a mix of the above Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95 © Copyright Virtual University of Pakistan 165 Introduction to Web Services Development (CS311) In the example above: , , , and have text content because they contain text (like 29.99). and have element contents, because they contain elements. has an attribute (category="children"). Empty XML Elements XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these. Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag. An element with no content is said to be empty. In XML, you can indicate an empty element like this: You can also use a so called self-closing tag: Syntax Following is the syntax to write an XML element:....content Where  element-name is the name of the element. The name its case in the start and end tags must match. © Copyright Virtual University of Pakistan 166 Introduction to Web Services Development (CS311)  attribute1, attribute2 are attributes of the element separated by white spaces. An attribute defines a property of the element. It associates a name with a value, which is a string of characters. An attribute is written as: name = "value" name is followed by an = sign and a string value inside double(" ") or single(' ') quotes. XML Elements Rules Following rules are required to be followed for XML elements:  An element name can contain any alphanumeric characters. The only punctuation mark allowed in names are the hyphen (-), under-score (_) and period (.).  Names are case sensitive. For example, Address, address, and ADDRESS are different names.  Start and end tags of an element must be identical.  An element, which is a container, can contain text or elements as seen in the above example. XML Attributes: Attributes are part of the XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair. Syntax An XML attribute has following syntax:....content.. < /element-name> © Copyright Virtual University of Pakistan 167 Introduction to Web Services Development (CS311) Where attribute1 and attribute2 has the following form: name = "value" Value has to be in double (" ") or single (' ') quotes. Here, attribute1 and attribute2 are unique attribute labels. Attributes are used to add a unique label to an element, place the label in a category, add a Boolean flag, or otherwise associate it with some string of data. Following example demonstrates the use of attributes: ]> Attributes are used to distinguish among elements of the same name. When you do not want to create a new element for every situation. Hence, use of an attribute can add a little more detail in differentiating two or more similar elements. In the above example, we have categorized the plants by including attribute category and assigning different values to each of the elements. Hence we have two categories of plants, one flowers and other color. Hence we have two plant elements with different attributes. © Copyright Virtual University of Pakistan 168 Introduction to Web Services Development (CS311) Element Attribute Rules Following are the rules that need to be followed for attributes:  An attribute name must not appear more than once in the same start-tag or empty-element tag.  An attribute must be declared in the Document Type Definition (DTD) using an Attribute- List Declaration.  Attribute values must not contain direct or indirect entity references to external entities.  The replacement text of any entity referred to directly or indirectly in an attribute value must not contain either less than sign < Viewing XML Files - Tove Jani Reminder Don't forget me this weekend! Look at the XML file above in your browser: note.xml Most browsers will display an XML document with color-coded elements. Often a plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. XSL = Style Sheets for XML XSLT is the most important part of XSL. © Copyright Virtual University of Pakistan 169 Introduction to Web Services Development (CS311) XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element. With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more. A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree. XSL consists of four parts:  XSLT - a language for transforming XML documents  XPath - a language for navigating in XML documents  XSL-FO - a language for formatting XML documents (discontinued in 2013)  XQuery - a language for querying XML documents Example: This example demonstrates the basics of setting up an XSLT transformation in a browser. The example will take an XML document that contains information (title, list of authors and body text) about an article and present it in a human readable form. Figure shows the source of the basic XSLT example. The XML document (example.xml) contains the information about the article. Using the ?xml-stylesheet? processing instruction, it links to the XSLT stylesheet (example.xsl) via its href attribute. An XSLT stylesheet starts with the xsl:stylesheet element, which contains all the templates used to create the final output. The example in Figure has two templates - one that matches the root node and one that matches Author nodes. The template that matches the root node outputs the © Copyright Virtual University of Pakistan 170 Introduction to Web Services Development (CS311) article's title and then says to process all templates that match Author nodes which are children of the Authors node. Figure: Simple XSLT Example XML Document (example.xml): My Article Mr. Foo Mr. Bar This is my article text. XSL Stylesheet (example.xsl): Article - Authors: - © Copyright Virtual University of Pakistan 171 Introduction to Web Services Development (CS311) Browser Output: Article - My Article Authors: - Mr. Foo - Mr. Bar XPath: XPath is used to navigate through elements and attributes in an XML document. XPath is a syntax for defining parts of an XML document. It uses path expressions to navigate in XML documents. XPath contains a library of standard functions. XPath is a major element in XSLT. XPath is a W3C recommendation XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath is a major element in the XSLT standard. Without XPath knowledge you will not be able to create XSLT documents. XPath is also used in XQuery, XPointer and XLink XLink: © Copyright Virtual University of Pakistan 172 Introduction to Web Services Development (CS311) Xml linking language, or xlink is an xml markup language and w3c specification that provides methods for creating internal and external links within xml documents, and associating metadata with those links. xlink provides a framework for creating both basic unidirectional links and more complex linking structures. Some important points about the xlink:  xlink is short for the xml linking language  xlink is a language for creating hyperlinks in xml documents  xlink is similar to html links - but it is a lot more powerful  xlink supports simple links(like html link system) and extended links (for linking multiple"more then one" resources together)  with xlink, the links can be defined outside of the linked files  xlink is a 'w3c recommendation' XLink Attribute Attribute Value Description xlink:actuate onLoad onRequest Defines when the linked resource is read and shown: other none  onLoad - the resource should be loaded and shown when the document loads  onRequest - the resource is not read or shown before the link is clicked © Copyright Virtual University of Pakistan 173 Introduction to Web Services Development (CS311) xlink:href URL Specifies the URL to link to xlink:show embed Specifies where to open the link. Default is "replace" new replace other none xlink:type simple Specifies the type of link extended locator arc resource title none Xpointer: xpointer is a system for addressing components of xml base internet media. xpointer language is divided among four specifications: a 'framework' which forms the basis for identifying xml fragments, a positional element addressing scheme, a scheme for namespaces, and a scheme for xpath-based addressing. There is no browser support for XPointer. But XPointer is used in other XML languages. Some important points about xpointer:  xpointer is short for the xml pointer language  xpointer uses xpath expressions to navigate in the xml document.  xpointer is a w3c recommendation © Copyright Virtual University of Pakistan 174 Introduction to Web Services Development (CS311) Module 12: (XML) XML Validator: XML validation is the process of checking a document written in XML (eXtensible Markup Language) to confirm that it is both well-formed and also "valid" in that it follows a defined structure. An XML document is said to be valid if its contents match with the elements, attributes and associated document type declaration (DTD), and if the document complies with the constraints expressed in it. Validation is dealt in two ways by the XML parser. They are:  Well-formed XML document  Valid XML document Well-formed XML document An XML document is said to be well-formed if it adheres to the following rules:  Non DTD XML files must use the predefined character entities for amp(&), apos(single quote), gt(>), lt(), lt( Example DTD: Valid XML: Valid XML: Use the #IMPLIED keyword if you don't want to force the author to include an attribute, and you don't have an option for a default value. #FIXED Syntax Example DTD: Valid XML: Invalid XML: Use the #FIXED keyword when you want an attribute to have a fixed value without allowing the author to change it. If an author includes another value, the XML parser will return an error. © Copyright Virtual University of Pakistan 185 Introduction to Web Services Development (CS311) Enumerated Attribute Values Syntax Example DTD: XML example: or Enumerated attribute values are used when you want the attribute value to be one of a fixed set of legal values. XML Elements vs. Attributes Data can be stored in child elements or in attributes. Examples: Anna Smith © Copyright Virtual University of Pakistan 186 Introduction to Web Services Development (CS311) female Anna Smith In the first example sex is an attribute. In the last, sex is a child element. Both examples provide the same information. There are no rules about when to use attributes, and when to use child elements. My experience is that attributes are handy in HTML, but in XML you should try to avoid them. Use child elements if the information feels like data. My Favorite Way I like to store data in child elements. The following three XML documents contain exactly the same information: A date attribute is used in the first example: Tove Jani Reminder Don't forget me this weekend! A date element is used in the second example: 12/11/2002 Tove © Copyright Virtual University of Pakistan 187 Introduction to Web Services Development (CS311) Jani Reminder Don't forget me this weekend! An expanded date element is used in the third: (THIS IS MY FAVORITE): 12 11 2002 Tove Jani Reminder Don't forget me this weekend! Some of the problems with attributes are:  attributes cannot contain multiple values (child elements can)  attributes are not easily expandable (for future changes)  attributes cannot describe structures (child elements can)  attributes are more difficult to manipulate by program code  attribute values are not easy to test against a DTD If attributes as containers for data are used, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data. © Copyright Virtual University of Pakistan 188 Introduction to Web Services Development (CS311) DTD – Entities Entities are used to define shortcuts to special characters. Entities can be declared internal or external. An Internal Entity Declaration Syntax Example DTD Example: XML example: &writer;&copyright; An entity has three parts: an ampersand (&), an entity name, and a semicolon (;). An External Entity Declaration Syntax DTD Example: © Copyright Virtual University of Pakistan 189 Introduction to Web Services Development (CS311) XML example: &writer;&copyright; XML Schema An XML Schema describes the structure of an XML document. The purpose of an XML Schema is to define the legal building blocks of an XML document:  the elements and attributes that can appear in a document  the number of (and order of) child elements  data types for elements and attributes  default and fixed values for elements and attributes The Element The element is the root element of every XML Schema:...... The element may contain some attributes. A schema declaration often looks something like this: © Copyright Virtual University of Pakistan 190 Introduction to Web Services Development (CS311)...... The following fragment: xmlns:xs="http://www.vu.edu.pk/XMLSchema" Indicates that the elements and data types used in the schema come from the “http://www.vu.edu.pk/XMLSchema" namespace. It also specifies that the elements and data types that come from the “http://www.vu.edu.pk/XMLSchema"namespace should be prefixed with xs: This fragment: targetNamespace=" http://www.vu.edu.pk" Indicates that the elements defined by this schema (note, to, from, heading, body.) come from the “http://www.vu.edu.pk"namespace. This fragment: xmlns="http://www.vu.edu.pk" © Copyright Virtual University of Pakistan 191 Introduction to Web Services Development (CS311) Indicates that the default namespace is “http://www.vu.edu.pk". This fragment: elementFormDefault="qualified" Indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified. XML on the Server XML can easily be stored and generated by a standard web server. XML files can be stored on an Internet server exactly the same way as HTML files. Start Windows Notepad and write the following lines: Jani Tove Remember me this weekend Generating XML with PHP XML can be generated on a server without any installed XML software. To generate an XML response from the server using PHP, use following code: The content type of the response header must be set to "text/xml". Generating XML with ASP To generate an XML response from the server - simply write the following code and save it as an ASP file on the web server: The content type of the response must be set to "text/xml". © Copyright Virtual University of Pakistan 193 Introduction to Web Services Development (CS311) Generating XML from a Database XML can be generated from a database without any installed XML software. To generate an XML database response from the server, simply write the following code and save it as an ASP file on the web server: Transforming XML with XSLT on the Server This ASP transforms an XML file to XHTML on the server: 0) { attnode = x[i].attributes; old_att = x[i].removeAttributeNode(attnode); } } Example explained: © Copyright Virtual University of Pakistan 235 Introduction to Web Services Development (CS311)  Suppose "books.xml" is loaded into xmlDoc  Use getElementsByTagName() to get all book nodes  For each book element check if there are any attributes  While there are attributes in a book element, remove the attribute Replace Nodes The replaceChild() method replaces a specified node. The nodeValue property replaces text in a text node. Replace an Element Node The replaceChild() method is used to replace a node. The following code fragment replaces the first element: Example xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement; //create a book element, title element and a text node newNode=xmlDoc.createElement("book"); newTitle=xmlDoc.createElement("title"); newText=xmlDoc.createTextNode("A Notebook"); //add the text node to the title node, newTitle.appendChild(newText); //add the title node to the book node newNode.appendChild(newTitle); y=xmlDoc.getElementsByTagName("book") //replace the first book node with the new node x.replaceChild(newNode,y); © Copyright Virtual University of Pakistan 236 Introduction to Web Services Development (CS311) Example explained:  Load "books.xml" into xmlDoc  Create a new element node  Create a new element node  Create a new text node with the text "A Notebook"  Append the new text node to the new element node  Append the new element node to the new element node  Replace the first element node with the new element node Replace Data In a Text Node The replaceData() method is used to replace data in a text node. The replaceData() method has three parameters:  offset - Where to begin replacing characters. Offset value starts at zero  length - How many characters to replace  string - The string to insert Example xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title").childNodes; x.replaceData(0,8,"Easy"); Example explained:  Load "books.xml" into xmlDoc  Get the text node of the first element node  Use the replaceData method to replace the eight first characters from the text node with "Easy" Use the nodeValue Property Instead © Copyright Virtual University of Pakistan 237 Introduction to Web Services Development (CS311) It is easier to replace the data in a text node using the nodeValue property. The following code fragment will replace the text node value in the first element with "Easy Italian": Example xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title").childNodes; x.nodeValue="Easy Italian"; Example explained:  Load "books.xml" into xmlDoc  Get the text node of the first element node  Use the nodeValue property to change the text of the text node Create a New Element Node The createElement() method creates a new element node: Example newElement = xmlDoc.createElement("edition"); xmlDoc.getElementsByTagName("book").appendChild(newElement); Example explained:  Suppose "books.xml" is loaded into xmlDoc  Create a new element node  Append the element node to the first element © Copyright Virtual University of Pakistan 238 Introduction to Web Services Development (CS311) Create a New Attribute Node The createAttribute() is used to create a new attribute node: Example newAtt = xmlDoc.createAttribute("edition"); newAtt.nodeValue = "first"; xmlDoc.getElementsByTagName("title").setAttributeNode(newAtt); Example explained:  Suppose "books.xml" is loaded into xmlDoc  Create a new attribute node "edition"  Set the value of the attribute node to "first"  Add the new attribute node to the first element Create an Attribute Using setAttribute() Since the setAttribute() method creates a new attribute if the attribute does not exist, it can be used to create a new attribute. Example exmlDoc.getElementsByTagName('book').setAttribute("edition","first"); Example explained:  Suppose "books.xml" is loaded into xmlDoc  Set the attribute "edition" value to "first" for the first element Create a Text Node The createTextNode() method creates a new text node: Example newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("first"); newEle.appendChild(newText); © Copyright Virtual University of Pakistan 239 Introduction to Web Services Development (CS311) xmlDoc.getElementsByTagName("book").appendChild(newEle); Example explained:  Suppose "books.xml" is loaded into xmlDoc  Create a new element node  Create a new text node with the text "first"  Append the new text node to the element node  Append the new element node to the first element Create a CDATA Section Node The createCDATASection() method creates a new CDATA section node. Example newCDATA = xmlDoc.createCDATASection("Special Offer & Book Sale"); xmlDoc.getElementsByTagName("book").appendChild(newCDATA); Example explained:  Suppose "books.xml" is loaded into xmlDoc  Create a new CDATA section node  Append the new CDATA node to the first element Loop through, and add a CDATA section, to all elements: Try it yourself Create a Comment Node The createComment() method creates a new comment node. Example newComment = xmlDoc.createComment("Revised March 2015"); xmlDoc.getElementsByTagName("book").appendChild(newComment); © Copyright Virtual University of Pakistan 240 Introduction to Web Services Development (CS311) Example explained:  Suppose "books.xml" is loaded into xmlDoc using  Create a new comment node  Append the new comment node to the first element Add a Node - appendChild() The appendChild() method adds a child node to an existing node. The new node is added (appended) after any existing child nodes. This code fragment creates an element (), and adds it after the last child of the first element: Example newEle = xmlDoc.createElement("edition"); xmlDoc.getElementsByTagName("book").appendChild(newEle); Example explained:  Suppose "books.xml" is loaded into xmlDoc  Create a new node  Append the node to the first element This code fragment does the same as above, but the new element is added with a value: Example newEle = xmlDoc.createElement("edition"); newText=xmlDoc.createTextNode("first"); newEle.appendChild(newText); xmlDoc.getElementsByTagName("book").appendChild(newEle); Example explained:  Suppose "books.xml" is loaded into xmlDoc  Create a new node  Create a new text node "first" © Copyright Virtual University of Pakistan 241 Introduction to Web Services Development (CS311)  Append the text node to the node  Append the node to the element Insert a Node - insertBefore() The insertBefore() method inserts a node before a specified child node. This method is useful when the position of the added node is important: Example newNode = xmlDoc.createElement("book"); x = xmlDoc.documentElement; y = xmlDoc.getElementsByTagName("book"); x.insertBefore(newNode,y); Example explained:  Suppose "books.xml" is loaded into xmlDoc  Create a new element node  Insert the new node in front of the last element node If the second parameter of insertBefore() is null, the new node will be added after the last existing child node. x.insertBefore(newNode,null) and x.appendChild(newNode) will both append a new child node to x. Add a New Attribute The setAttribute() method sets the value of an attribute. Example © Copyright Virtual University of Pakistan 242 Introduction to Web Services Development (CS311) xmlDoc.getElementsByTagName('book').setAttribute("edition","first"); Example explained:  Suppose "books.xml" has been loaded into xmlDoc  Set the value of the attribute "edition" to "first" for the first element If the attribute already exists, the setAttribute() method will overwrite the existing value. Add Text to a Text Node - insertData() The insertData() method inserts data into an existing text node. The insertData() method has two parameters:  offset - Where to begin inserting characters (starts at zero)  string - The string to insert The following code fragment will add "Easy" to the text node of the first element of the loaded XML: Example xmlDoc.getElementsByTagName("title").childNodes.insertData(0,"Easy "); Copy a Node The cloneNode() method creates a copy of a specified node. The cloneNode() method has a parameter (true or false). This parameter indicates if the cloned node should include all attributes and child nodes of the original node. The following code fragment copies the first node and appends it to the root node of the document: Example oldNode = xmlDoc.getElementsByTagName('book'); newNode = oldNode.cloneNode(true); xmlDoc.documentElement.appendChild(newNode); © Copyright Virtual University of Pakistan 243 Introduction to Web Services Development (CS311) Result: Everyday Italian Harry Potter XQuery Kick Start Learning XML Everyday Italian Example explained:  Suppose "books.xml" is loaded into xmlDoc  Get the node to copy (oldNode)  Clone the node into "newNode"  Append the new node to the the root node of the XML document Node Types The following table lists the different W3C node types, and which node types they may have as children: Node Type Description Children Element (max. one), Represents the entire document (the Document ProcessingInstruction, root-node of the DOM tree) Comment, DocumentType Element, Represents a "lightweight" Document ProcessingInstruction, DocumentFragment object, which can hold a portion of a Comment, Text, document CDATASection, EntityReference Provides an interface to the entities DocumentType None defined for the document ProcessingInstruction Represents a processing instruction None Element, ProcessingInstruction, EntityReference Represents an entity reference Comment, Text, CDATASection, EntityReference © Copyright Virtual University of Pakistan 244 Introduction to Web Services Development (CS311) Element, Text, Comment, ProcessingInstruction, Element Represents an element CDATASection, EntityReference Attr Represents an attribute Text, EntityReference Represents textual content in an Text None element or attribute Represents a CDATA section in a CDATASection document (text that will NOT be None parsed by a parser) Comment Represents a comment None Element, ProcessingInstruction, Entity Represents an entity Comment, Text, CDATASection, EntityReference Represents a notation declared in the Notation None DTD © Copyright Virtual University of Pakistan 245 Introduction to Web Services Development (CS311) Modue 14: Introduction to JAXP The Java API for XML Processing (JAXP) is for processing XML data using applications written in the Java programming language. JAXP leverages the parser standards Simple API for XML Parsing (SAX) and Document Object Model (DOM) so that you can choose to parse your data as a stream of events or to build an object representation of it. JAXP also supports the Extensible Stylesheet Language Transformations (XSLT) standard, giving you control over the presentation of the data and enabling you to convert the data to other XML documents or to other formats, such as HTML. JAXP also provides namespace support, allowing you to work with DTDs that might otherwise have naming conflicts. Finally, as of version 1.4, JAXP implements the Streaming API for XML (StAX) standard. Designed to be flexible, JAXP allows you to use any XML-compliant parser from within your application. It does this with what is called a pluggability layer, which lets you plug in an implementation of the SAX or DOM API. The pluggability layer also allows you to plug in an XSL processor, letting you control how your XML data is displayed. Overview of the Packages The SAX and DOM APIs are defined by the XML-DEV group and by the W3C, respectively. The libraries that define those APIs are as follows:  javax.xml.parsers: The JAXP APIs, which provide a common interface for different vendors' SAX and DOM parsers.  org.w3c.dom: Defines the Document class (a DOM) as well as classes for all the components of a DOM.  org.xml.sax: Defines the basic SAX APIs.  javax.xml.transform: Defines the XSLT APIs that let you transform XML into other forms. © Copyright Virtual University of Pakistan 246 Introduction to Web Services Development (CS311)  javax.xml.stream: Provides StAX-specific transformation APIs. SAX Packages The Simple API for XML (SAX) is the event-driven, serial-access mechanism that does element- by-element processing. The API for this level reads and writes XML to a data repository or the web. For server-side and high-performance applications, you will want to fully understand this level. But for many applications, a minimal understanding will suffice. DOM Packages The DOM API is generally an easier API to use. It provides a familiar tree structure of objects. You can use the DOM API to manipulate the hierarchy of application objects it encapsulates. The DOM API is ideal for interactive applications because the entire object model is present in memory, where it can be accessed and manipulated by the user. Comparison: On the other hand, constructing the DOM requires reading the entire XML structure and holding the object tree in memory, so it is much more CPU- and memory-intensive. For that reason, the SAX API tends to be preferred for server-side applications and data filters that do not require an in-memory representation of the data. The XSLT APIs defined in javax.xml.transform let you write XML data to a file or convert it into other forms. As shown in the XSLT section of this tutorial, you can even use it in conjunction with the SAX APIs to convert legacy data to XML. Finally, the StAX APIs defined in javax.xml.stream provide a streaming Java technology-based, event-driven, pull-parsing API for reading and writing XML documents. StAX offers a simpler programming model than SAX and more efficient memory management than DOM. © Copyright Virtual University of Pakistan 247 Introduction to Web Services Development (CS311) Simple API for XML APIs The basic outline of the SAX parsing APIs is shown below. To start the process, an instance of the SAXParserFactory class is used to generate an instance of the parser. The parser wraps a SAXReader object. When the parser's parse() method is invoked, the reader invokes one of several callback methods implemented in the application. Those methods are defined by the interfaces ContentHandler, ErrorHandler, DTDHandler, and EntityResolver. Summary of the key SAX APIs: SAX Parser Factory A SAXParserFactory object creates an instance of the parser determined by the system property, javax.xml.parsers.SAXParserFactory. SAX Parser © Copyright Virtual University of Pakistan 248 Introduction to Web Services Development (CS311) The SAXParser interface defines several kinds of parse() methods. In general, you pass an XML data source and a DefaultHandler object to the parser, which processes the XML and invokes the appropriate methods in the handler object. SAX Reader The SAXParser wraps a SAXReader. It is the SAXReader that carries on the conversation with the SAX event handlers you define. Default Handler A DefaultHandler implements the ContentHandler, ErrorHandler, DTDHandler, and EntityResolver interfaces (with null methods), so you can override only the ones you are interested in. Content Handler Methods such as startDocument, endDocument, startElement, and endElement are invoked when an XML tag is recognized. This interface also defines the methods characters() and processingInstruction(), which are invoked when the parser encounters the text in an XML element or an inline processing instruction, respectively. Error Handler Methods error(), fatalError(), and warning() are invoked in response to various parsing errors. The default error handler throws an exception for fatal errors and ignores other errors (including validation errors). Sometimes, the application may be able to recover from a validation error. Other times, it may need to generate an exception. To ensure the correct handling, you will need to supply your own error handler to the parser. DTD Handler Defines methods you will generally never be called upon to use. Used when processing a DTD to recognize and act on declarations for an unparsed entity. © Copyright Virtual University of Pakistan 249 Introduction to Web Services Development (CS311) Entity Resolver The resolveEntity method is invoked when the parser must identify data identified by a URI. In most cases, a URI is simply a URL, which specifies the location of a document, but in some cases the document may be identified by a URN - a public identifier, or name, that is unique in the web space. The public identifier may be specified in addition to the URL. The EntityResolver can then use the public identifier instead of the URL to find the document-for example, to access a local copy of the document if one exists. A typical application implements most of the ContentHandler methods, at a minimum. Because the default implementations of the interfaces ignore all inputs except for fatal errors, a robust implementation may also want to implement the ErrorHandler methods. SAX Packages The SAX parser is defined in the packages listed in the following Table. Packages Description org.xml.sax Defines the SAX interfaces. The name org.xml is the package prefix that was settled on by the group that defined the SAX API. org.xml.sax.ext Defines SAX extensions that are used for doing more sophisticated SAX processing-for example, to process a document type definition (DTD) or to see the detailed syntax for a file. org.xml.sax.helpers Contains helper classes that make it easier to use SAX-for example, by defining a default handler that has null methods for all the interfaces, so that you only need to override the ones you actually want to implement. © Copyright Virtual University of Pakistan 250 Introduction to Web Services Development (CS311) javax.xml.parsers Defines the SAXParserFactory class, which returns the SAXParser. Also defines exception classes for reporting errors. Document Object Model APIs Figure DOM APIs javax.xml.parsers.DocumentBuilderFactory class is used to get a DocumentBuilder instance, and that instance can also be used to produce a Document object that conforms to the DOM specification. The builder which is get, in fact, is determined by the system property javax.xml.parsers.DocumentBuilderFactory, which selects the factory implementation that is used to produce the builder. DocumentBuilder newDocument() method can also be used to create an empty Document that implements the org.w3c.dom.Document interface. Alternatively, you can use one of the builder's parse methods to create a Document from existing XML data. Although they are called objects, the entries in the DOM tree are actually fairly low-level data structures. For example, consider this structure: blue. There is an element node for the color tag, and under that there is a text node that contains the data, blue! © Copyright Virtual University of Pakistan 251 Introduction to Web Services Development (CS311) The Document Object Model implementation is defined in the packages listed in the following Table. Package Description org.w3c.dom Defines the DOM programming interfaces for XML (and, optionally, HTML) documents, as specified by the W3C. javax.xml.parsers Defines the DocumentBuilderFactory class and the DocumentBuilder class, which returns an object that implements the W3C Document interface. The factory that is used to create the builder is determined by the javax.xml.parsers system property, which can be set from the command line or overridden when invoking the new Instance method. This package also defines the ParserConfigurationException class for reporting errors. Overview SAX (the Simple API for XML) is an event-based parser for xml documents. Unlike a DOM parser, a SAX parser creates no parse tree. SAX is a streaming interface for XML, which means that applications using SAX receive event notifications about the XML document being processed an element, and attribute, at a time in sequential order starting at the top of the document, and ending with the closing of the ROOT element.  Reads an XML document from top to bottom, recognizing the tokens that make up a well- formed XML document  Tokens are processed in the same order that they appear in the document  Reports the application program the nature of tokens that the parser has encountered as they occur  The application program provides an "event" handler that must be registered with the parser  As the tokens are identified, callback methods in the handler are invoked with the relevant information © Copyright Virtual University of Pakistan 252 Introduction to Web Services Development (CS311) Content Handler Interface This interface specifies the callback methods that the SAX parser uses to notify an application program of the components of the XML document that it has seen.  void startDocument() - Called at the beginning of a document.  void endDocument() - Called at the end of a document.  void startElement(String uri, String localName, String qName, Attributes atts) - Called at the beginning of an element.  void endElement(String uri, String localName,String qName) - Called at the end of an element.  void characters(char[] ch, int start, int length) - Called when character data is encountered.  void ignorableWhitespace( char[] ch, int start, int length) - Called when a DTD is present and ignorable whitespace is encountered.  void processingInstruction(String target, String data) - Called when a processing instruction is recognized.  void setDocumentLocator(Locator locator)) - Provides a Locator that can be used to identify positions in the document.  void skippedEntity(String name) - Called when an unresolved entity is encountered.  void startPrefixMapping(String prefix, String uri) - Called when a new namespace mapping is defined.  void endPrefixMapping(String prefix) - Called when a namespace definition ends its scope. JDOM JDOM is an open source, java based library to parse XML document and it is typically java developer friendly API. It has a straightforward API, is a lightweight and fast, and is optimized for the Java programmer. It is java optimized, it uses java collection like List and Arrays. It works with DOM and SAX APIs and combines the better of the two. It is of low memory footprint and is nearly as fast as SAX. © Copyright Virtual University of Pakistan 253 Introduction to Web Services Development (CS311) Advantages JDOM gives java developers flexibility and easy maintainability of xml parsing code. It is light weight and quick API. JDOM classes The JDOM defines several Java classes. Here are the most common classes:  Document - Represents the entire XML document. A Document object is often referred to as a DOM tree.  Element - Represents an XML element. Element object has methods to manipulate its child elements,its text, attributes and namespaces.  Attribute Represents an attribute of an element. Attribute has method to get and set the value of attribute. It has parent and attribute type.  Text Represents the text of XML tag.  Comment Represents the comments in a XML document. Steps to Using JDOM Following are the steps used while parsing a document using JDOM Parser.  Import XML-related packages.  Create a SAXBuilder  Create a Document from a file or stream  Extract the root element  Examine attributes  Examine sub-elements Import XML-related packages import java.io.*; import java.util.*; import org.jdom2.*; © Copyright Virtual University of Pakistan 254 Introduction to Web Services Development (CS311) Create a DocumentBuilder SAXBuilder saxBuilder = new SAXBuilder(); Create a Document from a file or stream File inputFile = new File("input.txt"); SAXBuilder saxBuilder = new SAXBuilder(); Document document = saxBuilder.build(inputFile); Extract the root element Element classElement = document.getRootElement(); Examine attributes //returns specific attribute getAttribute("attributeName"); Examine sub-elements //returns a list of subelements of specified name getChildren("subelementName"); //returns a list of all child nodes getChildren(); //returns first child node getChild("subelementName"); JDOM XML parser modify an existing XML file: Add a new element Update existing element attribute Update existing element value Delete existing element © Copyright Virtual University of Pakistan 255 Introduction to Web Services Development (CS311) Example Add a new “age” element under staff Update the staff attribute id = 2 Update salary value to 7000 Delete “firstname” element under staff Introduction to JAXB Java Architecture for XML Binding (JAXB) provides a fast and convenient way to bind XML schemas and Java representations, making it easy for Java developers to incorporate XML data and processing functions in Java applications. As part of this process, JAXB provides methods for unmarshalling (reading) XML instance documents into Java content trees, and then marshalling (writing) Java content trees back into XML instance documents. JAXB also provides a way to generate XML schema from Java objects. JAXB 2.0 includes several important improvements to JAXB 1.0:  Support for all W3C XML Schema features. (JAXB 1.0 did not specify bindings for some of the W3C XML Schema features.)  Support for binding Java-to-XML, with the addition of the javax.xml.bind.annotation package to control this binding. (JAXB 1.0 specified the mapping of XML Schema-to-Java, but not Java-to-XML Schema.)  A significant reduction in the number of generated schema-derived classes.  Additional validation capabilities through the JAXP 1.3 validation APIs.  Smaller runtime libraries. © Copyright Virtual University of Pakistan 256 Introduction to Web Services Development (CS311) Architectural Overview The following figure shows the components that make up a JAXB implementation. Figure: JAXB Architectural Overview A JAXB implementation consists of the following architectural components:  Schema compiler: Binds a source schema to a set of schema-derived program elements. The binding is described by an XML-based binding language.  Schema generator: Maps a set of existing program elements to a derived schema. The mapping is described by program annotations. © Copyright Virtual University of Pakistan 257 Introduction to Web Services Development (CS311)  Binding runtime framework: Provides unmarshalling (reading) and marshalling (writing) operations for accessing, manipulating, and validating XML content using either schema-derived or existing program elements. The JAXB Binding Process The following figure shows what occurs during the JAXB binding process. Figure: Steps in the JAXB Binding Process The general steps in the JAXB data binding process are: 1. Generate classes: An XML schema is used as input to the JAXB binding compiler to generate JAXB classes based on that schema. © Copyright Virtual University of Pakistan 258 Introduction to Web Services Development (CS311) 2. Compile classes: All of the generated classes, source files, and application code must be compiled. 3. Unmarshal: XML documents written according to the constraints in the source schema are unmarshalled by the JAXB binding framework. Note that JAXB also supports unmarshalling XML data from sources other than files and documents, such as DOM nodes, string buffers, SAX sources, and so forth. 4. Generate content tree: The unmarshalling process generates a content tree of data objects instantiated from the generated JAXB classes; this content tree represents the structure and content of the source XML documents. 5. Validate (optional): The unmarshalling process involves validation of the source XML documents before generating the content tree. Note that if you modify the content tree in Step 6, you can also use the JAXB Validate operation to validate the changes before marshalling the content back to an XML document. 6. Process content: The client application can modify the XML data represented by the Java content tree by using interfaces generated by the binding compiler. 7. Marshal: The processed content tree is marshalled out to one or more XML output documents. The content may be validated before marshalling. More about Unmarshalling Unmarshalling provides a client application the ability to convert XML data into JAXB-derived Java objects. More about Marshalling Marshalling provides a client application the ability to convert a JAXB-derived Java object tree into XML data. By default, the Marshaller uses UTF-8 encoding when generating XML data. Client applications are not required to validate the Java content tree before marshalling. There is also no requirement that the Java content tree be valid with respect to its original schema to marshal it into XML data. © Copyright Virtual University of Pakistan 259 Introduction to Web Services Development (CS311) More about Validation Validation is the process of verifying that an XML document meets all the constraints expressed in the schema. JAXB 1.0 provided validation at unmarshal time and also enabled on-demand validation on a JAXB content tree. JAXB 2.0 only allows validation at unmarshal and marshal time. A web service processing model is to be lax in reading in data and strict on writing it out. To meet that model, validation was added to marshal time so users could confirm that they did not invalidate an XML document when modifying the document in JAXB form. XML Binding: XML data binding refers to a means of representing information in an XML document as a business object in computer memory. This allows applications to access the data in the XML from the object rather than using the DOM or SAX to retrieve the data from a direct representation of the XML itself. An XML data binder accomplishes this by automatically creating a mapping between elements of the XML schema of the document we wish to bind and members of a class to be represented in memory. When this process is applied to convert an XML document to an object, it is called unmarshalling. The reverse process, to serialize an object as XML, is called marshalling. JAXB is Java Architecture for XML Binding JAXB, stands for Java Architecture for XML Binding, using JAXB annotation to convert

Use Quizgecko on...
Browser
Browser