skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, October 18, 2012

Using JDOM to send request and decode response

Posted by Raju Gupta at 4:00 AM – 0 comments
 

Describes how JDOM can be used to prepare request and also to decode a response   


// Packages 
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;

/* 
Sample request to be constructed
<Car xmlns="http://xml.abc.com/CAR_2_10">
 <Maruti>
  <Model>Swift<Model>
 <Maruti>
<Car>

Sample response to be decoded
<Car xmlns="http://xml.abc.com/CAR_2_10">
 <Maruti>
  <Swift>
   <Price>550000</Price>
   <Color>Blue<Color>
  <Swift>
 <Maruti>
<Car>
*/

//Request construction using JDOM
Element car = new Element("Car");
Namespace ns = Namespace.getNamespace ("http://xml.abc.com/CAR_2_10");
car.setNamespace(ns);
Element maruti = new Element("Maruti", ns);
Element model = new Element("Model", ns);
model.setText("Swift");
maruti.addContent(model);
car.addContent(maruti);

//Converting Document to String
new XMLOutputter(indent, true).output(document, new StringWriter());

//Decoding a response using JDOM

/*********************Option 1*********************************/
String price = null;
String color = null;
SAXBuilder builder = new SAXBuilder();
StringReader reader = new StringReader(xmlString);
Document doc = builder.build(reader);
Element root = doc.getRootElement();
Namespace ns = Namespace.getNamespace("http://xml.abc.com/CAR_2_10");
Element swift = getRecursiveChild(root, "maruti/swift", ns);
price = swift.getChild("price", ns).getText();
color = swift.getChild("color", ns).getText();

// JDOM does not provide any method to reach into a child under multiple indentations. This method helps to reach into a Grandchild node 
public static Element getRecursiveChild(Element start, String path, Namespace ns) {
        StringTokenizer names = new StringTokenizer(path, "/");
        while (start != null && names.hasMoreTokens()) {
            start = start.getChild(names.nextToken(), ns);
        }
        return start;
}

/***********************Option 2************************************/
//Although option 1 is a better way of reading a Document, it might not be supported in older versions of JDOM. In those cases we can use this option.

String price = null;
String color = null;
SAXBuilder builder = new SAXBuilder();
StringReader reader = new StringReader(xmlString);
Document doc = builder.build(reader);
Element root = doc.getRootElement();
Namespace ns = Namespace.getNamespace("http://xml.abc.com/CAR_2_10");
Iterator itr = (root.getChildren()).iterator();
while (itr.hasNext()) {
     Element maruti = (Element) itr.next();
     if (!maruti.getName().equals("Maruti"))
         continue;
     Iterator marutiItr = (maruti.getChildren()).iterator();
     while (marutiItr.hasNext()) {
         Element swift = (Element) marutiItr.next();
          if (!swift.getName().equals("swift"))
   continue;
     Iterator swiftItr = (swift.getChildren()).iterator();
     while (swiftItr.hasNext()) {
         Element childElement = (Element) swiftItr.next();
          if (childElement.getName().equals("price"))
   price = childElement.getText();
          else (childElement.getName().equals("color"))
   color = childElement.getText();
 }
   }
}
 




Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger