XML parsing with XPath

Parsing and extracting data from XML file with XPath.
The output displays detailed information on all books whose price is higher than 10, which were published after 2005.

bookCatalog.xml

1:  <?xml version="1.0" encoding="UTF-8"?>  
2:  <!DOCTYPE root SYSTEM "bookCatalogDTD.dtd">  
3:    
4:  <catalog>  
5:    <book id="bk101">  
6:     <author>Gambardella, Matthew</author>  
7:     <title>XML Developer's Guide</title>  
8:     <genre>Computer</genre>  
9:     <price>44.95</price>  
10:     <publish_date>2000-10-01</publish_date>  
11:     <description>An in-depth look at creating applications   
12:     with XML.</description>  
13:    </book>  
14:    <book id="bk102">  
15:     <author>Ralls, Kim</author>  
16:     <title>Midnight Rain</title>  
17:     <genre>Fantasy</genre>  
18:     <price>5.95</price>  
19:     <publish_date>2002-12-16</publish_date>  
20:     <description>A former architect battles corporate zombies,   
21:     an evil sorceress, and her own childhood to become queen   
22:     of the world.</description>  
23:    </book>  
24:    <book id="bk103">  
25:     <author>Corets, Eva</author>  
26:     <title>Maeve Ascendant</title>  
27:     <genre>Fantasy</genre>  
28:     <price>5.95</price>  
29:     <publish_date>2000-11-17</publish_date>  
30:     <description>After the collapse of a nanotechnology   
31:     society in England, the young survivors lay the   
32:     foundation for a new society.</description>  
33:    </book>  
34:    <book id="bk104">  
35:     <author>Corets, Eva</author>  
36:     <title>Oberon's Legacy</title>  
37:     <genre>Fantasy</genre>  
38:     <price>5.95</price>  
39:     <publish_date>2001-03-10</publish_date>  
40:     <description>In post-apocalypse England, the mysterious   
41:     agent known only as Oberon helps to create a new life   
42:     for the inhabitants of London. Sequel to Maeve   
43:     Ascendant.</description>  
44:    </book>  
45:    <book id="bk105">  
46:     <author>Corets, Eva</author>  
47:     <title>The Sundered Grail</title>  
48:     <genre>Fantasy</genre>  
49:     <price>5.95</price>  
50:     <publish_date>2001-09-10</publish_date>  
51:     <description>The two daughters of Maeve, half-sisters,   
52:     battle one another for control of England. Sequel to   
53:     Oberon's Legacy.</description>  
54:    </book>  
55:    <book id="bk106">  
56:     <author>Randall, Cynthia</author>  
57:     <title>Lover Birds</title>  
58:     <genre>Romance</genre>  
59:     <price>4.95</price>  
60:     <publish_date>2003-09-02</publish_date>  
61:     <description>When Carla meets Paul at an ornithology   
62:     conference, tempers fly as feathers get ruffled.</description>  
63:    </book>  
64:    <book id="bk107">  
65:     <author>Thurman, Paula</author>  
66:     <title>Splish Splash</title>  
67:     <genre>Romance</genre>  
68:     <price>4.95</price>  
69:     <publish_date>2004-11-02</publish_date>  
70:     <description>A deep sea diver finds true love twenty   
71:     thousand leagues beneath the sea.</description>  
72:    </book>  
73:    <book id="bk108">  
74:     <author>Knorr, Stefan</author>  
75:     <title>Creepy Crawlies</title>  
76:     <genre>Horror</genre>  
77:     <price>4.95</price>  
78:     <publish_date>2005-12-06</publish_date>  
79:     <description>An anthology of horror stories about roaches,  
80:     centipedes, scorpions and other insects.</description>  
81:    </book>  
82:    <book id="bk109">  
83:     <author>Kress, Peter</author>  
84:     <title>Paradox Lost</title>  
85:     <genre>Science Fiction</genre>  
86:     <price>6.95</price>  
87:     <publish_date>2006-11-02</publish_date>  
88:     <description>After an inadvertant trip through a Heisenberg  
89:     Uncertainty Device, James Salway discovers the problems   
90:     of being quantum.</description>  
91:    </book>  
92:    <book id="bk110">  
93:     <author>O'Brien, Tim</author>  
94:     <title>Microsoft .NET: The Programming Bible</title>  
95:     <genre>Computer</genre>  
96:     <price>36.95</price>  
97:     <publish_date>2006-12-09</publish_date>  
98:     <description>Microsoft's .NET initiative is explored in   
99:     detail in this deep programmer's reference.</description>  
100:    </book>  
101:    <book id="bk111">  
102:     <author>O'Brien, Tim</author>  
103:     <title>MSXML3: A Comprehensive Guide</title>  
104:     <genre>Computer</genre>  
105:     <price>36.95</price>  
106:     <publish_date>2007-12-01</publish_date>  
107:     <description>The Microsoft MSXML3 parser is covered in   
108:     detail, with attention to XML DOM interfaces, XSLT processing,   
109:     SAX and more.</description>  
110:    </book>  
111:    <book id="bk112">  
112:     <author>Galos, Mike</author>  
113:     <title>Visual Studio 7: A Comprehensive Guide</title>  
114:     <genre>Computer</genre>  
115:     <price>49.95</price>  
116:     <publish_date>2008-04-16</publish_date>  
117:     <description>Microsoft Visual Studio 7 is explored in depth,  
118:     looking at how Visual Basic, Visual C++, C#, and ASP+ are   
119:     integrated into a comprehensive development   
120:     environment.</description>  
121:    </book>  
122:  </catalog>  
123:    
124:    

Main.java

1:  /*  
2:   * Djordje Gavrilovic  
3:   */  
4:  package xmla01;  
5:    
6:  import java.io.File;  
7:  import java.io.FileInputStream;  
8:  import java.io.FileNotFoundException;  
9:  import javax.xml.parsers.DocumentBuilderFactory;  
10:  import javax.xml.xpath.XPath;  
11:  import javax.xml.xpath.XPathConstants;  
12:  import javax.xml.xpath.XPathExpression;  
13:  import javax.xml.xpath.XPathExpressionException;  
14:  import javax.xml.xpath.XPathFactory;  
15:  import org.w3c.dom.NodeList;  
16:  import org.xml.sax.InputSource;  
17:    
18:  public class Main {  
19:    public static void main(String[] args) throws XPathExpressionException, FileNotFoundException {  
20:        // experiment com
21:      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
22:      dbf.setIgnoringElementContentWhitespace(true);  
23:        
24:      XPathFactory factory = XPathFactory.newInstance();  
25:      XPath path = factory.newXPath();  
26:      XPathExpression xPE = path.compile("//book[price>10 and translate(publish_date,'-','')>20050000]");  
27:        
28:      File xmlDoc = new File("bookCatalog.xml");  
29:      InputSource source = new InputSource(new FileInputStream(xmlDoc));  
30:        
31:      Object result = xPE.evaluate(source, XPathConstants.NODESET);  
32:      NodeList nList = (NodeList)result;  
33:        
34:      for (int i = 0; i < nList.getLength(); i++) {  
35:        System.out.print(nList.item(i).getNodeName() + " ");  
36:        System.out.println(nList.item(i).getAttributes().item(0));  
37:        System.out.println(" Author: " + nList.item(i).getFirstChild().getNextSibling().getTextContent());  
38:        System.out.println(" Title: " + nList.item(i).getFirstChild().getNextSibling().  
39:            getNextSibling().getNextSibling().getTextContent());  
40:        System.out.println(" Genre: " + nList.item(i).getFirstChild().getNextSibling().  
41:            getNextSibling().getNextSibling().getNextSibling().getNextSibling().getTextContent());  
42:        System.out.println(" Price: " + nList.item(i).getLastChild().getPreviousSibling().  
43:            getPreviousSibling().getPreviousSibling().getPreviousSibling().getPreviousSibling().getTextContent());  
44:        System.out.println(" Publish date: " + nList.item(i).getLastChild().getPreviousSibling().  
45:            getPreviousSibling().getPreviousSibling().getTextContent());  
46:        System.out.println(" Description: " + nList.item(i).getLastChild().getPreviousSibling().getTextContent());  
47:        System.out.print("\n");  
48:      }  
49:        
50:    }  
51:      
52:  }  
53:    


Comments

  1. your code generates this on Run:
    Exception in thread "main" java.io.FileNotFoundException: books.xml (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.(FileInputStream.java:138)
    at main.Main.main(Main.java:44)
    Java Result: 1

    ReplyDelete
  2. Best casinos in Las Vegas, NV, USA
    Find the best slots 양산 출장안마 at Best 고양 출장안마 Casino 아산 출장안마 in Las Vegas, NV. Try your luck at the 바카라 사이트 best casino in Las Vegas. Claim your bonuses, 당진 출장안마 make your winnings, and play your favorite slot

    ReplyDelete

Post a Comment