29 |
|
|
30 |
|
//---CONSTRUCTORS--- |
31 |
|
|
32 |
< |
// Constructor for accepting XML input. |
32 |
> |
/** |
33 |
> |
* Constructor for accepting XML input. |
34 |
> |
* |
35 |
> |
* @param xml A String of XML to process. |
36 |
> |
*/ |
37 |
|
public XMLPacketMaker (String xml) { |
38 |
|
_xml = xml; |
39 |
|
} |
40 |
|
|
41 |
|
//---PUBLIC METHODS--- |
42 |
|
|
43 |
< |
public XMLPacket createXMLPacket() { |
43 |
> |
/** |
44 |
> |
* Method to create an XML packet from the data this |
45 |
> |
* class was constructed with. |
46 |
> |
* |
47 |
> |
* @return an XMLPacket representing the XML String given |
48 |
> |
* @throws InvalidXMLException if the XML cannot be parsed |
49 |
> |
*/ |
50 |
> |
public XMLPacket createXMLPacket() throws InvalidXMLException { |
51 |
|
|
52 |
|
// Create the XMLPacket to store values in. |
53 |
|
XMLPacket packet = new XMLPacket(); |
55 |
|
// Use the default (non-validating) parser |
56 |
|
SAXParserFactory factory = SAXParserFactory.newInstance(); |
57 |
|
try { |
47 |
– |
|
58 |
|
// Parse the input |
59 |
|
InputSource inputSource = new InputSource(new StringReader(_xml)); |
60 |
|
SAXParser saxParser = factory.newSAXParser(); |
61 |
|
saxParser.parse(inputSource, new XMLStringParser(packet)); |
52 |
– |
|
62 |
|
} |
63 |
|
catch (Exception e) { |
64 |
< |
return null; |
64 |
> |
// couldn't parse the XML for some reason |
65 |
> |
throw new InvalidXMLException("Could not parse the XML: "+_xml); |
66 |
|
} |
67 |
< |
|
67 |
> |
// parsed successfully, return the packet |
68 |
|
return packet; |
69 |
|
} |
70 |
|
|