/Users/lyon/j4p/src/xml/cart/Xml2Cart.java
|
1 package xml.cart;
2
3 import futils.Futil;
4 import org.xml.sax.SAXException;
5 import org.xml.sax.SAXParseException;
6
7 import javax.xml.parsers.ParserConfigurationException;
8 import javax.xml.parsers.SAXParser;
9 import javax.xml.parsers.SAXParserFactory;
10 import java.io.IOException;
11
12 import xml.cart.Cart;
13 import xml.cart.CartDocumentHandler;
14
15 public class Xml2Cart {
16
17 public static void main(String argv [])
18 throws IOException {
19 try {
20
21 getCart();
22
23 } catch (SAXParseException e) {
24 System.out.println(
25 "** Parsing error"
26 + ", line " + e.getLineNumber()
27 + ", uri " + e.getSystemId()
28 + " " + e.getMessage()
29 );
30
31 } catch (SAXException e) {
32 e.printStackTrace();
33 } catch (ParserConfigurationException e) {
34 e.printStackTrace();
35 }
36 }
37
38 private static void getCart()
39 throws SAXParseException, SAXException,
40 ParserConfigurationException, IOException {
41 Cart c = read("file:" +
42 Futil.getReadFile(
43 "select an XML file").getAbsolutePath());
44 System.out.println("Cart=" + c);
45 }
46
47 public static Cart read(String uri)
48 throws SAXParseException,
49 SAXException,
50 ParserConfigurationException,
51 IOException {
52 SAXParserFactory spf =
53 SAXParserFactory.newInstance();
54 spf.setValidating(true);
55 SAXParser sp = spf.newSAXParser();
56
57 CartDocumentHandler dh = new CartDocumentHandler();
58 sp.parse(uri, dh);
59 return dh.getCart();
60 }
61 }
62
63
64
65