[jdom-interest] Problem with Iteration (again)
Edd Dawson
E.J.Dawson at derby.ac.uk
Thu Jul 15 01:43:51 PDT 2004
Hi
I contacted this list a week ago about iterating through an XML
document, the following is an example of the XML :
<Response>
<InboundMessage>
<Ticket>1278</Ticket>
<MessageText>Example 1</MessageText>
<Phone>+4409878656787</Phone>
<Date>123456123</Date>
</InboundMessage>
<InboundMessage>
<Ticket>1279</Ticket>
<MessageText>Example 2</MessageText>
<Phone>+4409878656787</Phone>
<Date>123456123</Date>
</InboundMessage>
<InboundMessage>
<Ticket>1280</Ticket>
<MessageText>Example 3</MessageText>
<Phone>+4409878656787</Phone>
<Date>123456123</Date>
</InboundMessage>
</Request>
I use the following code to pick it up :
public String pickUpReceivedMessages() {
String xml = "<?xml
version='1.0'?><Request><Authentication><Username>username</Username><Password>password</Password></Authentication><RetrieveInbound><Retrieve>All</Retrieve></RetrieveInbound></Request>";
StringBuffer buffer = new StringBuffer();
String inputLine;
try {
//--------open connection to the xml gateway
URL urlObj = new URL(TXTTOOLS_URL);
HttpURLConnection con =
(HttpURLConnection)urlObj.openConnection();
//--------set method to POST
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
//--------write out the xml
PrintStream outStream = new PrintStream(con.getOutputStream());
outStream.print("XMLPost=" + URLEncoder.encode(xml, "UTF-8"));
outStream.flush();
//--------read in the returned xml from server
InputStream in = con.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(in));
//--------close streams and return
//in.close();
outStream.close();
ReceivedSMSDAO receivedSMSDAO = new
ReceivedSMSDAO(classHostName,classHostPort,classSid,classUsername,classPassword);
SAXBuilder builder = new SAXBuilder();
Document doc = null;
try {
doc = builder.build(reader);
}
catch(Exception ex) {
return "Error on making xml returned SAXable" +
ex.getMessage();
}
in.close();
// start with some root element...
Element oEleRoot = doc.getRootElement();
// get down to Response element
Element oEleResponse = oEleRoot.getChild("Response");
// get all children called InboundMessage
List oListInBoundMsg = null;
try {
oListInBoundMsg =
oEleResponse.getChildren("InboundMessage");
} catch (Exception ex) {
return "error on making list : " + ex.getMessage();
}
// create Iterator
Iterator oIterator = oListInBoundMsg.iterator();
String oEleTicket;
String oEleMessageText;
String oElePhone;
String oEleDate;
//Loop:
while (oIterator.hasNext()) {
//derive Reference from current iterator
Element oEleNext = (Element) oIterator.next();
oEleTicket = oEleNext.getChild("Ticket").getTextTrim();
oEleMessageText =
oEleNext.getChild("MessageText").getTextTrim();
oElePhone = oEleNext.getChild("Phone").getTextTrim();
oEleDate = oEleNext.getChild("Date").getTextTrim();
receivedSMSDAO.insertSMS(oEleTicket,oEleMessageText,oElePhone,oEleDate);
}// while...
}
catch(Exception ex) {
return "error on POST : " + ex.getMessage();
}
}
I get an error with the bit of code which populates my list :
try {
oListInBoundMsg =
oEleResponse.getChildren("InboundMessage");
} catch (Exception ex) {
return "error on making list : " + ex.getMessage();
}
The error i get on the console is as follows :
"error on making list : null"
So although it creates a document fine it doesn't let me query it.... do
you have any ideas?
cheers
Edd
More information about the jdom-interest
mailing list