简体   繁体   English

SimpleXML-Android-CDATA解析

[英]SimpleXML - Android - CDATA parsing

I'm using SimpleXML for parsing XML files in Android. 我正在使用SimpleXML在Android中解析XML文件。 I need to parse the following XML, 我需要解析以下XML,

<?xml version="1.0" encoding="UTF-8"?>
<Box>
    <SerialNumber>XYSSDSD</SerialNumber>
    <Alias><![CDATA[SSS: 8]]></Alias>
    <BoxType>SD</BoxType>
</Box>

I wrote Bean class to map the above xml 我写了Bean类来映射上面的xml

@Element(name="SerialNumber")
private String serialNumber;
@Element(name="Alias", data=true)
private String aliasType;  
@Element(name="BoxType")
private String boxType;

I'm getting the following exception while parsing the XML 解析XML时出现以下异常

1-24 23:57:47.407: E/Exception in APItoBEAN(1796): Unable to satisfy @org.simpleframework.xml.Element(data=true, name=Alias, required=true, type=void) on field 'aliasType' private .Box.aliasType for class Box at line 1 1-24 23:57:47.407:APItoBEAN(1796)中的E / Exception:无法满足字段'aliasType上的@ org.simpleframework.xml.Element(data = true,name = Alias,required = true,type = void)第1行的Box类的private .Box.aliasType

Could you anybody help me here? 有人可以帮我吗? Thank you for your time! 感谢您的时间!

I tested this on PC (SimpleXML 2.6.6) with following Java Code: 我在PC(SimpleXML 2.6.6)上使用以下Java代码对此进行了测试:

Box Class: 箱类:

@Root
public class Box
{
    @Element(name = "SerialNumber")
    private String serialNumber;
    @Element(name = "Alias", data = true)
    private String aliasType;
    @Element(name = "BoxType")
    private String boxType;

    // ...
}

Reading the XML: 读取XML:

final File f = new File("test.xml"); // your XML is in this file


Serializer ser = new Persister();
Box box = ser.read(Box.class, f);

Works without exception. 没有例外。

Did you clean your project and build it again? 您是否清理过项目并再次构建? Your code seems to be OK. 您的代码似乎没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM