9 |
|
|
10 |
|
|
11 |
|
public class XMLPacketMaker extends HandlerBase { |
12 |
+ |
|
13 |
+ |
public XMLPacketMaker () { |
14 |
+ |
// default no-args constructor. |
15 |
+ |
} |
16 |
+ |
|
17 |
+ |
// Constructor for accepting a reference to an XMLPacket |
18 |
+ |
public XMLPacketMaker (XMLPacket packet) { |
19 |
+ |
this.packet = packet; |
20 |
+ |
} |
21 |
+ |
|
22 |
|
public static void main(String[] args){ |
23 |
|
if (args.length != 1) { |
24 |
|
System.err.println ("Usage: cmd filename"); |
31 |
|
// Set up output stream |
32 |
|
out = new OutputStreamWriter (System.out, "UTF8"); |
33 |
|
|
34 |
+ |
// Create the XMLPacket to store values in. |
35 |
+ |
packet = new XMLPacket(); |
36 |
+ |
|
37 |
|
// Parse the input |
38 |
|
SAXParser saxParser = factory.newSAXParser(); |
39 |
< |
saxParser.parse(new File(args[0]), new XMLPacketMaker()); |
39 |
> |
saxParser.parse(new File(args[0]), new XMLPacketMaker(packet)); |
40 |
|
|
41 |
+ |
// Print out some things from the packet: - |
42 |
+ |
|
43 |
+ |
|
44 |
|
} catch (Throwable t) { |
45 |
|
t.printStackTrace (); |
46 |
|
} |
53 |
|
|
54 |
|
// For storing the tag heirarchy. |
55 |
|
private ArrayList tagList = new ArrayList(); |
56 |
+ |
static private XMLPacket packet = null; |
57 |
|
|
58 |
|
//=========================================================== |
59 |
|
// SAX DocumentHandler methods |
79 |
|
|
80 |
|
public void startElement (String name, AttributeList attrs) throws SAXException { |
81 |
|
indentLevel++; |
82 |
+ |
tagList.add(name); |
83 |
|
nl(); emit ("ELEMENT: "); |
84 |
|
emit ("<"+name); |
85 |
|
if (attrs != null) { |
100 |
|
nl(); |
101 |
|
emit ("END_ELM: "); |
102 |
|
emit ("</"+name+">"); |
103 |
+ |
tagList.remove(tagList.size()-1); |
104 |
|
indentLevel--; |
105 |
|
} |
106 |
|
|
107 |
|
public void characters (char[] buf, int offset, int len) throws SAXException { |
108 |
|
nl(); emit ("CHARS: "); |
109 |
|
String s = new String(buf, offset, len); |
110 |
< |
if (!s.trim().equals("")) emit (s); |
110 |
> |
if (!s.trim().equals("")) { |
111 |
> |
emit (s); |
112 |
> |
packet.addParam((String)tagList.get(tagList.size()-1), s); |
113 |
> |
} |
114 |
|
} |
115 |
|
|
116 |
|
//=========================================================== |