开发者

xml parsing from server with html tags

I am new in android development.i send you xml file,it contain some html tag.that is follows- How i parse text,image. XMLFILE-

<NewDataSet>
<Table><Cat_id>1</Cat_id><Cat_Parentid>0</Cat_Parentid><Cat_Name>News for the day</Cat_Name><Cat_Desc><div style="text-align: center;"><span class="Apple-style-span" style="font-weight: normal; font-size: medium; "></span></div><span class="Apple-style-span" style="color: rgb(105, 105, 105); font-family: Verdana; font-size: 13px; font-weight: normal; "><br><div style="text-align: center;">Yes we are coming at E & I? Are you?</div></span><br><h1 style="font-weight: bold; "><span style="font-family: Verdana; font-size: 14pt; ">News for the day...</span></h1><span style="color: rgb(105, 105, 105); "><span style="font-family: Tahoma; font-size: 10pt; ">Template Mobile Sites for IC: <a href="http://icmobilesite.zapak_vi.net/">http://icmobilesite.zapak_vi.net/</a></span><br><span class="Apple-style-span" style="font-family: Tahoma; font-size: 13px; color: rgb(105, 105, 105); ">Promotional valid till 30 Sept 2011: MOBILE WEBSITE (Base Product Mobile CMS) for JUST $159<br></span></span><br><span style="font-family: Tahoma; font-size: 10pt; color: rgb(105, 105, 105); ">Mobile Template link: </span><a href="http://icmobilesite.zapak_vi.net/"><span class="Apple-style-span" style="font-size: 15px; font-family: Calibri, sans-serif; color: rgb(105, 105, 105); ">http://newsletter.zapak_vi.net/Mobilesite/</span><br></a><span style="font-size: 10pt; font-family: Tahoma; "><span style="font-size: 10pt; "><br><span style="color: rgb(105, 105, 105); ">With the promotion on Business Edge and eFusion still running successful in e market place - $ 499</span><br><span style="color: rgb(105, 105, 105); ">Check out some of the latest site launch on: </span><br><br><span style="color: rgb(105, 105, 105); font-weight: bold; ">http://www.randallcontracting.co.uk/Pages/Default.aspx </span><br><br><span style="color: rgb(105, 105, 105); font-size: 10pt; "><span style="font-weight: bold; ">Category</span>: Building & Construction</span><br></span><br></span><div style="color: rgb(105, 105, 105); text-align: left; "><span style="font-size: 10pt; font-family: Tahoma; ">Description: WELCOME TO RANDALL CONTRACTING Randall Contracting is a family-run contracting SME which has been servicing London and the South East since 1956. Working closely with our Clients and external Design Consultants, we place great emphasis on a safe, positive, practical and common sense approach to our projects. Our delivery methods have resulted in an extensive volume of repeat business from both Private and Public Sectors. Safety and Environmental concerns are a high priority on all our contracts and we continually strive to开发者_StackOverflow社区 source innovative working methods and solutions. Our equipment is regularly updated and maintained to ensure minimal environmental impact.</span><br><br></div><span style="color: rgb(105, 105, 105); font-weight: bold; font-family: Tahoma; "><span style="font-size: 10pt; "><br></span></span><br></Cat_Desc><Cat_Active>true</Cat_Active><Cat_SortOrder>1</Cat_SortOrder><langid>1</langid><proCatImage>1bag6.jpg</proCatImage><bigimage>no-img.gif</bigimage></Table>
<NewDataSet>

END OF FILE


That same thing i done with parsing.

XML Handler class-

class xmlHandler extends DefaultHandler {
    private Vector nodes = new Vector();
    private Stack tagStack = new Stack();

    public void startDocument() throws SAXException {
    }

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // System.out.println("1");
        if (qName.equals("clause")) {
            Noden node = new Noden();
            nodes.addElement(node);
            // System.out.println("2");
        }
        // System.out.println("3");
        tagStack.push(qName);
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
        String chars = new String(ch, start, length).trim();

        if (chars.length() > 0) {
            String qName = (String) tagStack.peek();
            Noden currentnode = (Noden) nodes.lastElement();
            // System.out.println(qName);

            if (qName.equals("en")) {
                currentnode.setEn(chars);
            } else if (qName.equals("alt")) {
                currentnode.setAlt(chars);
            }
        }
    }

    public void endElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        tagStack.pop();
    }

    public void endDocument() throws SAXException {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < nodes.size(); i++) {
            Noden currentnode = (Noden) nodes.elementAt(i);
            result.append("En : " + currentnode.getEn() + " Alt : "
                    + currentnode.getAlt() + "\n");
        }
        System.out.println(result.toString());
    }

    class Noden {
        private String en;
        private String alt;

        public Noden() {
        }

        public void setEn(String en) {
            this.en = en;
        }

        public void setAlt(String alt) {
            this.alt = alt;
        }

        public String getEn() {
            return en;
        }

        public String getAlt() {
            return alt;
        }
    };

retriving-

try
        {
          SAXParserFactory factory = SAXParserFactory.newInstance();
          SAXParser saxParser = factory.newSAXParser();
          InputStream is = null;

          String xml = "your pasable string";

          String newXml = xml.substring(xml.indexOf("<translation>"), xml.indexOf("]]"));         
          System.out.println(newXml);
          is = new ByteArrayInputStream(newXml.getBytes());
          InputSource inputSource = new InputSource(is);
          saxParser.parse(inputSource,new xmlHandler());
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }

This will helps you.


Use this way

public class HTMLData_XHandler extends DefaultHandler

{

boolean in_root;
boolean in_subtag1;
boolean in_subtag2;

@Override
public void startDocument() throws SAXException
{
    super.startDocument();
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
    super.startElement(uri, localName, qName, attributes);
    if (localName.equals("root"))
    {
        in_root = true;
    } else if (localName.equals("subtag1"))
    {
        in_subtag1 = true;
    } else if (localName.equals("subtag2"))
    {
        in_subtag2 = true;
    }

}

@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
    super.characters(ch, start, length);
    String data = new String(ch, start, length);

    if (in_subtag2)
    {
        array_list.add(data);
    }
    if (in_subtag1)
    {
        array_list.add(data);
    }

}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
    super.endElement(uri, localName, qName);

    if (localName.equals("root"))
    {
        in_root = false;
    } else if (localName.equals("subtag1"))
    {
        in_subtag1 = false;
    } else if (localName.equals("subtag2"))
    {
        in_subtag2 = false;
    }
}

@Override
public void endDocument() throws SAXException
{
    super.endDocument();
}

}

array_list variable is for storing data from tags. you can choose any kind of variable, as your wish.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜