jjzjj

安卓 : Cdata in xml not parsing correctly(sax)

coder 2024-07-03 原文

我是 android 开发的新手,我正在尝试创建一个从 Web 服务中提取 xml 的应用程序。

除了 Cdata 标记中的位之外,所有其余的 xml 都可以正常提取,而不是提取大量的 html 文本,而是提取“<p>”。有人能给我指出正确的方向,说明为什么它没有正确拉入吗

这是我的xml

<articles>
   <article>
    <name>AndroidPeople</name>
    <headline>notandroidpeople</headline>
    <website category="android">www.androidpeople.com</website>
    <htmltext><![CDATA[<p>
  HAVING lost to Manchester City and drawn with Manchester United, Yohan Cabaye is confident Newcastle are capable of rounding off a testing treble by beating Chelsea provided they reproduce their
  performance from Old Trafford.
</p>
<p>
  The Magpies drew 1-1 at the weekend, with Demba Ba's penalty a just reward for their efforts even if it owed much to a fortuitous decision from referee Mike Jones.
</p>
<p>
  Whereas they were outclassed for periods of the previous weekend's defeat at the Etihad Stadium, Saturday's game saw them more than match a Manchester United side who remain comfortably ensconced
  in second position.
</p>
<p>
  And with fifth-placed Chelsea having lost three of their last five league games, Cabaye feels Newcastle will claim a notable scalp provided they do not allow their performance levels to dip from
  the standards they established at the weekend.
</p>
<p>
  "We learned from the City game," said the French midfielder, who could resume his partnership with Cheik Tiote in three days time if the Ivory Coast international finally completes his recovery
  from a knee problem. "We learned not to give Man United too much respect.
</p>
<p>
  "It was important to play higher up the pitch and I think we did well. Against Chelsea, we have to play like we did at Old Trafford."
</p>
<p>
  While they have not beaten Chelsea in a league game since 2006, Newcastle are entitled to go into Saturday's lunch-time kick-off in a confident mood given their record of four wins and two draws
  from their six home games this season.
</p>
<p>
  "We have to believe in ourselves," said Cabaye. "It's important to win the game. We've got to play like the winning team. We will be at home so we have to play like a top-of-the-league team to win
  this match."
</p>
<p>
  Chelsea go into this weekend's game ten points adrift of league leaders Manchester City, and while last weekend's 3-0 win over Wolves lifted some of the pressure on Andre Villas-Boas, the Londoners
  cannot afford to drop more points on their visit to the North-East.
</p>
<p>
  Their title hopes have suffered a series of significant blows in the last few weeks, and Cabaye feels the battle for the Premier League crown is already a two-horse race.
</p>
<p>
  "I think the two Manchester clubs will be the top two for the rest of the season," he said. "I think they will be the teams fighting for the league.
</p>
<p>
  "Are City better? I don't know. It's hard to compare the two. Man United have some very good players, so it is difficult to simply say that Man City are better than Man United."
</p>]]></htmltext>

    </article>

    <article>
    <name>iPhoneAppDeveloper</name>
    <headline>notiphonepeople</headline>
    <website category="iPhone">www.iphone-app-developer.com</website>
    <htmltext><![CDATA[<p>
  HAVING lost to Manchester City and drawn with Manchester United, Yohan Cabaye is confident Newcastle are capable of rounding off a testing treble by beating Chelsea provided they reproduce their
  performance from Old Trafford.
</p>
<p>
  The Magpies drew 1-1 at the weekend, with Demba Ba's penalty a just reward for their efforts even if it owed much to a fortuitous decision from referee Mike Jones.
</p>
<p>
  Whereas they were outclassed for periods of the previous weekend's defeat at the Etihad Stadium, Saturday's game saw them more than match a Manchester United side who remain comfortably ensconced
  in second position.
</p>
<p>
  And with fifth-placed Chelsea having lost three of their last five league games, Cabaye feels Newcastle will claim a notable scalp provided they do not allow their performance levels to dip from
  the standards they established at the weekend.
</p>
<p>
  "We learned from the City game," said the French midfielder, who could resume his partnership with Cheik Tiote in three days time if the Ivory Coast international finally completes his recovery
  from a knee problem. "We learned not to give Man United too much respect.
</p>
<p>
  "It was important to play higher up the pitch and I think we did well. Against Chelsea, we have to play like we did at Old Trafford."
</p>
<p>
  While they have not beaten Chelsea in a league game since 2006, Newcastle are entitled to go into Saturday's lunch-time kick-off in a confident mood given their record of four wins and two draws
  from their six home games this season.
</p>
<p>
  "We have to believe in ourselves," said Cabaye. "It's important to win the game. We've got to play like the winning team. We will be at home so we have to play like a top-of-the-league team to win
  this match."
</p>
<p>
  Chelsea go into this weekend's game ten points adrift of league leaders Manchester City, and while last weekend's 3-0 win over Wolves lifted some of the pressure on Andre Villas-Boas, the Londoners
  cannot afford to drop more points on their visit to the North-East.
</p>
<p>
  Their title hopes have suffered a series of significant blows in the last few weeks, and Cabaye feels the battle for the Premier League crown is already a two-horse race.
</p>
<p>
  "I think the two Manchester clubs will be the top two for the rest of the season," he said. "I think they will be the teams fighting for the league.
</p>
<p>
  "Are City better? I don't know. It's hard to compare the two. Man United have some very good players, so it is difficult to simply say that Man City are better than Man United."
</p>]]></htmltext>

    </article>
</articles>

这是我的代码

    import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = null;
    public static SitesList sitesList = null;



    public static SitesList getSitesList() {
        return sitesList;
    }

    public static void setSitesList(SitesList sitesList) {
        MyXMLHandler.sitesList = sitesList;

    }

    /** Called when tag starts ( ex:- <name>AndroidPeople</name> 
     * -- <name> )*/
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        currentElement = true;

        if (localName.equals("articles"))
        {
            /** Start */ 
            sitesList = new SitesList();
        } else if (localName.equals("website")) {
            /** Get attribute value */
            String attr = attributes.getValue("category");
            sitesList.setCategory(attr);
        }
        else if (localName.equals("htmlText")){

        }

    }

    /** Called when tag closing ( ex:- <name>AndroidPeople</name> 
     * -- </name> )*/
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currentElement = false;

        /** set value */ 
        if (localName.equalsIgnoreCase("name"))
            sitesList.setName(currentValue);
        else if (localName.equalsIgnoreCase("headline"))
            sitesList.setHeadline(currentValue);
        else if (localName.equalsIgnoreCase("website"))
            sitesList.setWebsite(currentValue);
        else if (localName.equalsIgnoreCase("htmltext"))
            sitesList.setHtmltext(currentValue);

    }

    /** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
     * -- to get AndroidPeople Character ) */
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
                        currentElement = false;
        }

    }

}

链接到 xml 文件 here

最佳答案

您的问题的解决方案是在读取字符时使用 StringBuffer 并在您的 endElement 中使用 buffer.toString() 获取所有数据.以下是一些代码 fragment :

    private StringBuffer buffer;

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        buffer = new StringBuffer();
        // TODO: your other code
    }

    public void characters(char[] ch,int start, int length) throws SAXException{
        String readChars = new String(ch,start,length);
        if(buffer != null) buffer.append(readChars);
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        currentValue  = buffer.toString();
        // TODO: your other code
    }

希望对您有所帮助。

关于安卓 : Cdata in xml not parsing correctly(sax),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324486/

有关安卓 : Cdata in xml not parsing correctly(sax)的更多相关文章

  1. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  2. (一)专题介绍:移动端安卓手机改造成linux服务器&linux服务器中安装软件、部署前后端分离项目实战 - 2

    快捷目录前言一、涉及到的相关技术简介二、具体实现过程及踩坑杂谈1.安卓手机改造成linux系统实现方案2.改造后的手机Linux中软件的安装3.手机Linux中安装MySQL5.7踩坑实录4.手机Linux中安装软件的正确方法三、Linux服务器部署前后端分离项目流程1.前提准备(安装必要软件,搭建环境):2.前后端分离项目的详细部署过程:总结前言总体概述:本篇文章隶属于“手机改造服务器部署前后端分离项目”系列专栏,该专栏将分多个板块,每个板块独立成篇来详细记录:手机(安卓)改造成个人服务器(Linux)、Linux中安装软件、配置开发环境、部署JAVA+VUE+MySQL5.7前后端分离项目

  3. 微信小程序安卓视频播放卡顿问题 - 2

    在微信小程序开发中遇到在video组件的两个问题1.安卓手机里播放视频会有明显的卡顿问题刚开始以为是网络问题,或者是视频文件问题。排查了一下发现都没问题最后加了个属性就OK了uniapp和原生小程序方法:custom-cache="false"custom-cache={{false}}video组件兼容iOS手机custom-cache加了这个属性会让2.iOS手机第一次播放视频会有几秒黑屏问题因此我加了当前手机型号的判断uni.getDeviceInfo().deviceType获取当前设备api当为iPhone时不加custom-cache属性,否则加上custom-cache=“fal

  4. 安卓渐变的背景框实现 - 2

    安卓渐变的背景框实现1.背景实现方法1.利用PorterDuffXfermode进行图层的混合,这是最推荐的方法,也是最有效的。2.利用canvas裁剪实现,这个方法有个缺陷,就是圆角会出现毛边,也就是锯齿。3.利用layer绘制边框1.背景万恶的设计小姐姐又来搞事情啦,你说好好的设计一个纯色的背景框框不好嘛,非要把一个框框弄成渐变的,如果不拿出放大镜估计没几个人能看出来它是渐变的。来,我让你看看是啥样框子是从左到右渐变的,设计应该是做了一个底图,然后上面盖了一个白色圆角矩形。那么我们该怎么去实现它呢?实现方法下面介绍三种实现它的方法。先贴上源码地址,大家记得给个starhttps://git

  5. 安卓手机浏览器:远程调试 - 2

    简介:有时我们需要调试手机浏览器网页信息,这时除了使用fiddler抓包拦截篡改,还可以通过USB连接,通过PC远程调试手机上的浏览器信息,进行映射。历史攻略:adb:安卓手机USB调试模式前置准备:网页内容在移动设备上的体验可能和电脑上完全不同。ChromeDevTools提供远程调试功能安卓远程调试支持:在浏览器选项卡中调试网站。在原生安卓应用中调试网页内容。将屏幕从你的安卓设备上投影到你的开发机器上。使用端口转发和虚拟主机映射来让安卓设备访问开发使用的服务器。操作步骤:1、手机通过USB连接电脑。2、开启手机调试模式。3、PC电脑edge输入:edge://inspect/#device

  6. 安卓性能优化之内存优化 - 2

    Java对象生命周期:创建:为对象分配内存空间,构造对象应用:此时对象至少被一个强引用持有不可见:未被任何强引用持有,进行可达性分析不可达:可达性分析为不可达,进入下一阶段收集:当垃圾回收器发现该对象已经处于“不可达阶段”并且垃圾回收器已经对该对象的内存空间重新分配做好准备时,则对象进入了“收集阶段”。如果该对象已经重写了finalize()方法,则会去执行该方法的终端操作。终结:当对象执行完finalize()方法后仍然处于不可达状态时(可达性分析垃圾回收算法被回收前,会有两次标记过程,判断是否执行lfinalize()方法,执行完之后判断是否GCROOT可达,如果仍不可达,则准备回收),则

  7. Qt安卓开发:调用java代码的获取usb权限 - 2

    最近换了工作,新工作是负责用qml做qt安卓开发。工作中遇到一个问题:安卓设备有USB口,需要插入一个U盘在程序里读写U盘中的文件,由于安卓系统的安全性的问题导致QFile、c++的文件操作相关方法都不能读写成功,想要读写成功只能调用java代码,在java代码里面使用安卓的DocumentFile库。经过一番探索,成功解决了问题。qt如何添加java代码不说了,网上有。下面是具体的java代码:packagecom.example.myapplication;importandroid.annotation.TargetApi;importandroid.content.Context;im

  8. Obsidian安卓端同步及使用(Remotely Save+阿里云同步S3) - 2

    Obsidian安卓端同步及使用(RemotelySave+阿里云同步S3)强烈推荐的obsidian的markdown教程​obsidian这款软件很不错,最近刚入门,用来做笔记,喜欢在电脑上做笔记,手机端能随时查看,故捣鼓了一下安卓端的同步及安卓端的使用1.安装包获取​不能科学上网,我是到官方中文论坛上找到的,网址如下:移动端v1.4.1开始测试-Obsidian中文论坛2.电脑端同步+阿里云配置​我使用的是RemotelySave插件​首先,电脑端关闭安全模式,下载这个第三方插件,登不上的看这里,网址如下:完美解决obsidian无法加载第三方插件(社区插件)的问题​然后就是阿里云的同步

  9. java - 如何使用 SAX 增加 entityExpansionLimit 来解析 XML 文件 - 2

    我正在尝试使用Java中的SAX解析器解析一个1.23GB的XML文件。我正在使用Mac操作系统和JDK1.7.0.51。不幸的是,我收到以下错误:Thepasrserhasencounteredmorethan"64000"entityexpansionsinthisdocument;thisisthelimitimposedbytheJDK.有人建议我扩展实体扩展,但不知道该怎么做。谁能帮我解决这个问题。非常感谢您的帮助。 最佳答案 我找到了解决这个问题的方法。它现在正在和我一起工作。我正在使用Eclipse。以下是步骤:1-R

  10. 安卓 XML 错误 : no resource identifier found for attribute 'xmlns' in package 'android' - 2

    我知道这里有一百个问题和我一样,但似乎没有一个适合我的具体问题,所以我要问一个新问题。以防万一这是重复,我很抱歉。所以,我正在构建一个应用程序,布局给我带来了一些问题。这是我的XML代码:(尚未完成)我得到的错误是在代码的第一行它说“错误:在包'android'中找不到属性'xmlns'的资源标识符我一遍又一遍地检查代码,尝试刷新/重建项目,尝试删除该特定行等等,但似乎没有任何解决办法。那么,如果有人有一些想法?谢谢! 最佳答案 删除android:xmlns="http://schemas.android.com/apk/res/

随机推荐