10-09-2012 01:14 AM
<?xml version="1.0" encoding="UTF-8"?>
<app id="TestApp">
<page id="home" >
<h_box id="h_box_1" width="100%" height="30%" bgcolor="#3399FF">
<v_box id="v_box_1" width="50%" height="100%" bgcolor="#8258FA">
<control id="article1_item1" background="images/article1_image2.jpg"
class="image"
height="40%"
marginleft="2%"
marginright="2%"
margintop="2%"
onclick="page1"
width="90%" >
</control>
<control id="article1_item1" background="images/article1_image1.jpg"
class="image"
height="40%"
marginleft="2%"
marginright="2%"
margintop="2%"
onclick="page1"
width="90%" >
</control>
</v_box>
<v_box id="v_box_2" width="50%" height="100%" bgcolor="#04B4AE">
<control id="article1_item1" background="images/article2_image1.jpg"
class="image"
height="40%"
marginleft="2%"
marginright="2%"
margintop="2%"
onclick="page1"
width="90%" >
</control>
<control id="article1_item1" background="images/article2_image2.jpg"
class="image"
height="40%"
marginleft="2%"
marginright="2%"
margintop="2%"
onclick="page1"
width="90%" >
</control>
</v_box>
</h_box>
<v_box id="v_box_4" width="100%" height="45%" bgimage="images/bg.png">
<control id="article1_item1" background="images/article_1_image_1.jpg"
class="image"
height="65%"
marginleft="10%"
marginright="2%"
margintop="2%"
onclick="page1"
width="75%" >
</control>
<control
id="top1"
class="text"
height="60%"
marginleft="2%"
marginright="2%"
text="The Egyptian military says it has launched missile strikes from helicopters against suspected Islamist militants in the Sinai peninsula."
textcolor="#ffffff"
fontweight="italic"
fontsize="7px"
width="98%" >
</control>
</v_box>
<h_box id="v_box_3" width="100%" height="25%" bgcolor="#502F2F">
<control id="article1_item1" background="images/article3_image1.jpg"
class="image"
height="85%"
marginleft="2%"
marginright="2%"
margintop="2%"
onclick="page1"
width="30%" >
</control>
<control id="article1_item1" background="images/article3_image2.jpg"
class="image"
height="85%"
marginleft="2%"
marginright="2%"
margintop="2%"
onclick="page1"
width="30%" >
</control>
<control id="article1_item1" background="images/article_2_image_3.jpg"
class="image"
height="85%"
marginleft="2%"
marginright="2%"
margintop="2%"
onclick="page1"
width="30%" >
</control>
<control id="article1_item1" background="images/article_6_image_2.jpg"
class="image"
height="85%"
marginleft="2%"
marginright="2%"
margintop="2%"
onclick="page1"
width="30%" >
</control>
<control id="article1_item1" background="images/block_1_social_1.jpg"
class="image"
height="85%"
marginleft="2%"
marginright="2%"
margintop="2%"
onclick="page1"
width="30%" >
</control>
<control id="article1_item1" background="images/article_7_image_3.jpg"
class="image"
height="85%"
marginleft="2%"
marginright="2%"
margintop="2%"
onclick="page1"
width="30%" >
</control>
</h_box>
</page>
</app>This is my XML.I am able to parse it using DOM parser,bt i m new to SAX parsing.Can somebody help me to parse it using SAX parser
Solved! Go to Solution.
10-09-2012 03:10 AM
There is a Code snippet for Calling Xml from HTTP request & than to parse it using Sax Parser.
SAXParserImpl saxparser = new SAXParserImpl();
ListParser receivedListHandler=new ListParser();
static DataInputStream din = null;
public static String response;
HttpConnection hc = null;
OutputStream os;
try
{
final String url ="<Enter URL for Xml Http Address>"+ InitClass.getConnectionString()+";ConnectionTimeou t=25000";
hc = (HttpConnection)Connector.open(url);
os = hc.openOutputStream();
os.write(postDataBytes);
if (hc.getResponseCode() == 200)
din = hc.openDataInputStream();
else
response = "" + hc.getResponseCode();
saxparser.parse(din, receivedListHandler);
}
catch (Exception e)
{
}
finally
{
try
{
if(din!=null)
din.close();
din = null;
if(hc!=null)
hc.close();
hc = null;
}
catch (Exception e) { }
}
parser Class
public class ListParser extends DefaultHandler
{
private String Key="";
private Hashtable ht=new Hashtable();
vector vec = new Vector();
public ListParser()
{
}
/**
* Gets called, whenever a Xml is start .
*/
public void startDocument() throws SAXException
{
}
/**
* Gets called, whenever a Xml is End .
*/
public void endDocument() throws SAXException
{
}
/**
* Gets called, whenever a new tag is found.
*/
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException
{
if(name.equals("<tag name>"))
{
ht = null;
ht = new Hashtable();
}
else if(name.equals("<tag Name>"))
{
}
Key=name;
}
/**
* Gets called, whenever a closed tag is found.
*/
public void endElement(String uri, String localName, String name) throws SAXException
{
if(name.equals("<tag name>"))
{
vec.addElement(ht);
}
}
public void characters(char[] ch, int start, int length) throws SAXException
{
String element=new String(ch, start, length);
ht.put(Key, element);
}
}
It will parse your XML and Will provide you data in vector vec in hashtables for per XML tag.
10-09-2012 04:37 AM
Thanx man
10-16-2012 07:56 AM
<?xml version="1.0" encoding="UTF-8"?>
<app id="Home">
<page id="home" transition="left" width="100%" height="100%">
<h_box id="h_box_4" width="100%" height="50%" bgcolor="#143BFF">
<control id="article1_item1" background="images/article_2_image_1.jpg"
class="image"
height="70%"
marginleft="10px"
marginright="5px"
margintop="5px"
onclick="page1"
width="70%" >
</control>
</h_box>
<h_box id="h_box_4" width="100%" height="50%" bgcolor="#D021A7">
<control id="article1_item1" background="images/article_1_image_1.jpg"
class="image"
height="40%"
marginleft="10px"
marginright="5px"
margintop="5px"
onclick="page1"
width="80%" >
</control>
</h_box>
</page>
</app>This is my present XML.When the first <h_box> tag ends,it goes to the portion from where it is called instead of parsing the 2nd <h_box> tag.My code for parsing is below
package mypackage;
import java.util.Hashtable;
import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MainParser extends DefaultHandler
{
private String Key="";
private Hashtable ht= null;;
Vector vec=null ;
public String parentTag;
public String mainTag;
public MainParser()
{
}
/**
* Gets called, whenever a Xml is start .
*/
public void startDocument() throws SAXException
{
}
/**
* Gets called, whenever a Xml is End .
*/
public void endDocument() throws SAXException
{
}
/**
* Gets called, whenever a new tag is found.
*/
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException
{
String key = null;
ht = new Hashtable();
vec = new Vector();
for (int i = 0; i < attributes.getLength(); i++) {
key = attributes.getLocalName(i);
String val = attributes.getValue(i);
ht.put(key, val);
}
if(!(name.equalsIgnoreCase("control"))){
MyScreen.parentTag=Key;
}
//MyScreen.parentTag=Key;
if(name.equals("page"))
{
MyScreen.getPageData(ht);
}
else if(name.equals("v_box"))
{
if(MyScreen.parentTag.equalsIgnoreCase("page")){
mainTag="v_box";
parentTag="v_box";
}
else {
MyScreen.parentTag=name;
parentTag=name;
}
MyScreen.getData(ht);
if(MyScreen.backgrndcolor!=null){
MyScreen.vfm = new CustomVerticalFieldManager(MyScreen.wd, MyScreen.ht,
MyScreen.color);
}
else {
MyScreen.vfm = new CustomVerticalFieldManager(MyScreen.wd, MyScreen.ht,MyScreen.padding,
MyScreen.backgroundImg);
}
MyScreen.backgrndcolor=null;
}
else if(name.equals("h_box"))
{
if( MyScreen.parentTag.equalsIgnoreCase("page")){
mainTag="h_box";
parentTag="h_box";
}
else {
MyScreen.parentTag=name;
parentTag=name;
}
MyScreen.getData(ht);
if(MyScreen.backgrndcolor!=null){
MyScreen.hfm = new CustomHorizontalFieldManager(MyScreen.wd, MyScreen.ht,
MyScreen.color);
}
else {
MyScreen.hfm = new CustomHorizontalFieldManager(MyScreen.wd, MyScreen.ht,MyScreen.padding,
MyScreen.backgroundImg);
}
MyScreen.backgrndcolor=null;
}
if(name.equals("control"))
{
//MyScreen.parentTag=parentTag;
MyScreen.getControlData(ht);
}
Key=name;
}
/**
* Gets called, whenever a closed tag is found.
*/
public void endElement(String uri, String localName, String name) throws SAXException
{
if(name.equals("h_box"))
{
if(mainTag.equalsIgnoreCase("h_box")){
MyScreen.hfm.add(MyScreen.hfm);
}
else {
MyScreen.vfm.add(MyScreen.hfm);
}
MyScreen.parentTag=mainTag;
}
else if(name.equals("v_box"))
{
if(mainTag.equalsIgnoreCase("v_box")){
MyScreen.vfm.add(MyScreen.vfm);
}
else {
MyScreen.hfm.add(MyScreen.vfm);
}
MyScreen.parentTag=mainTag;
}
else if(name.equals("control"))
{
// MyScreen.parentTag=mainTag;
}
}
}Please help
10-16-2012 08:50 AM
can anyone tell me where i am doing wrong in the parsing
10-16-2012 12:06 PM