CSC 308 2.0 System Development with Java Working with XML Department of Statistics and Computer Science
What is XML XML stands for extensible Markup Language is designed to transport and store data XML was designed to carry data, not to display data XML tags are not predefined. You must define your own tags XML is designed to be self-descriptive XML is important to know, and very easy to learn Example <?xml version="1.0"?> <note> <to>student</to> <from>budditha</from> <heading>reminder</heading> <body>about Final Assignment</body> </note> 2
XML document use a self-describing and simple syntax <?xml version="1.0"?> <note> <to>student</to> <from>budditha</from> <heading>reminder</heading> XML declaration root element <body>about Final Assignment</body> </note> child element 3
Example <bookstore> <book category="cooking"> <title lang="en">everyday Italian</title> <author>giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> </bookstore> Source: w3school 4
What is XPath XPath is used to navigate through elements and attributes in an XML document XPath uses path expressions to navigate in XML documents Uses path expressions to select nodes or nodesets in an XML document There are functions for string values, numeric values, date and time comparison XPath contains a library of standard functions XPath includes over 100 built-in functions 5
XPath Terminology There are seven kinds of nodes Element Attribute Text Namespace Processing Instruction Comment Document nodes <?xml version="1.0" encoding="iso-8859-1"?> <bookstore> <book> <title lang="en">harry Potter</title> <author>j K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> 6
XPath Syntax Selecting Nodes Expression nodename Description Selects all child nodes of the named node / Selects from the root node // Selects nodes in the document from the current node that match the selection no matter where they are. Selects the current node.. Selects the parent of the current node @ Selects attributes 7
Example Path Expression bookstore /bookstore <?xml version="1.0" encoding="iso-8859-1"?> <bookstore> <book> <title lang="en">harry Potter</title> <author>j K. Rowling</author> </book> </bookstore> Result Selects all the child nodes of the bookstore element Selects the root element bookstorenote: If the path starts with a slash ( / ) it always represents an absolute path to an element! bookstore/book //book bookstore//book Selects all book elements that are children of bookstore Selects all book elements no matter where they are in the document Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element //@lang Selects all attributes that are named lang 8
Predicates Predicates are used to find a specific node or a node that contains a specific value Path Expression /bookstore/book[1] /bookstore/book[last()] /bookstore/book[last()-1] /bookstore/book[position( )<3] Result Selects the first book element that is the child of the bookstore element.note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1] Selects the last book element that is the child of the bookstore element Selects the last but one book element that is the child of the bookstore element Selects the first two book elements that are children of the bookstore element 9
Predicates contd.. Path Expression //title[@lang] //title[@lang='eng'] /bookstore/book[price> 35.00] /bookstore/book[price> 35.00]/title Result Selects all the title elements that have an attribute named lang Selects all the title elements that have an attribute named lang with a value of 'eng' Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00 10
Selecting Unknown Nodes XPath wildcards can be used to select unknown XML elements Wildcard Description * Matches any element node @* Matches any attribute node node() Matches any node of any kind Path Expression /bookstore/* Result Selects all the child nodes of the bookstore element //* Selects all elements in the document //title[@*] Selects all title elements which have any attribute 11
Selecting Several Paths Using the operator in an XPath expression you can select several paths Path Expression //book/title //book/price //title //price /bookstore/book/title //price Result Selects all the title AND price elements of all book elements Selects all the title AND price elements in the document Selects all the title elements of the book element of the bookstore element AND all the price elements in the document 12
XML with JAVA 13
XML with Java Java JDK, two built-in XML parsers (DOM and SAX)are available DOM is the easiest to use Java XML Parser DOM Parser is slow and consume a lot memory SAX parser is work differently with DOM parser the SAX parser use callback function Java classes javax.xml.parsers.documentbuilderfactory; javax.xml.parsers.documentbuilder; org.w3c.dom.document; org.w3c.dom.nodelist; org.w3c.dom.node; org.w3c.dom.element; java.io.file; Etc.. 14
Java-Create new XML document private void createxmldocument() DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try DocumentBuilder db = dbf.newdocumentbuilder(); Document dom = db.newdocument(); catch(parserconfigurationexception pce) System.out.println("Error " + pce); 15
Java-load XML document private void loadxmldocument() try DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newdocumentbuilder(); dom = db.parse(filename); catch (ParserConfigurationException ex) 16
Create Document Tree private void createdomtree() Element rootele = dom.createelement(" bookstore"); dom.appendchild(rootele); // mydata is a list object Iterator it = mydata.iterator(); while(it.hasnext()) bookobject b = (bookobject )it.next(); Element BookEle = createnewbookelement(b); rootele.appendchild(bookele); 17
Create Book Element private Element createbookelement(bookobject b) Element bookele = dom.createelement( book"); Element titleele = dom.createelement( title"); titleele.setattribute( lang", b.getlan()); titleele.settextcontent(b.getlantext()); AgentEle.appendChild(titleEle); Element autele = dom.createelement("author"); autele.settextcontent(b.getautname()); bookele.appendchild(autele); return AgentEle; <book> <title lang="en"> Computer Science </title> <author>b Hettige</author> </book> 18
Class bookobject class bookobject private String lan; private String aut; private String title; public bookobject(string na, String nlan, String ntit) lan = nlan; aut = na; title = ntit; public String getlan() return lan; public String getaut() return aut; public String gettitle() return title; 19
Read Elements public void getbookauthor() try DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance(); domfactory.setnamespaceaware(true); DocumentBuilder builder = domfactory.newdocumentbuilder(); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xpath.compile("bookstore/book/author"); Object result = expr.evaluate(dom, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println(" Number of items : " + nodes.getlength()); for (int i = 0; i < nodes.getlength(); i++) System.out.println("Authors " +nodes.item(i).gettextcontent()); catch (ParserConfigurationException ex) 20
Write to XML File public void writetoxmlfile() try OutputFormat format = new OutputFormat(dom); format.setindenting(true); XMLSerializer serializer = new XMLSerializer( new FileOutputStream(new File( bookdata.xml )), format); serializer.serialize(dom); catch(ioexception ie) System.out.println(ie); 21
Set Attributes public void changebookauthor(string oldaut, String newaut) try DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance(); domfactory.setnamespaceaware(true); DocumentBuilder builder = domfactory.newdocumentbuilder(); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xpath.compile("bookstore/book[author='"+oldaut +"']/author"); Object result = expr.evaluate(dom, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println(" Number of items : " + nodes.getlength()); for (int i = 0; i < nodes.getlength(); i++) System.out.println("Old Authors " +nodes.item(i).gettextcontent()); nodes.item(i).settextcontent(newaut); System.out.println("New Authors " +nodes.item(i).gettextcontent()); catch (ParserConfigurationException ex) 22
Main Method XMLObjectExample public static void main(string[] args) XMLObject xml = new XMLObject(); xml.addnewbook(new bookobject("budditha", "English", "Computer Science")); xml.addnewbook(new bookobject("saman", "Sinhala", "Language ")); xml.update(); xml.getbookauthor(); xml.changebookauthor("budditha", "bud"); xml.getbookauthor(); xml.writetoxmlfile(); 23