从XML解析服务器的HTML标记 [英] xml parsing from server with html tags

查看:149
本文介绍了从XML解析服务器的HTML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android的development.i新的送你的XML文件,它包含一些HTML tag.that是如下 -
我如何解析文本,图像。
XMLFILE -

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 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>

文件结束

推荐答案

这同样的事情我解析。做

That same thing i done with parsing.

XML处理程序类 -

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();
        }

这将帮助您。

这篇关于从XML解析服务器的HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆