如何在标记中使用xml-parsing标记+文本(简单框架)

时间:2014-01-08 05:30:47

标签: java xml-parsing tags simple-framework

如何在标记中使用xml解析标记+文本(简单框架)http://simple.sourceforge.net/resources.php

我想打印这个  :xliffTestModel = XliffTestModel [attname = hours,g =%1 $ s,text = hours !!]

我无法解析“小时!!”

[XML]

<?xml version="1.0" encoding="utf-8"?>

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

    <string name="hours">
        <xliff:g id="hours">%1$s</xliff:g> hours!!
    </string>

</resources>


这是我的代码。

[模型]

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "resources")
public class XMXliffStringModel {
    @Element(name = "string")
    public XliffTestModel xliffTestModel;

    @Override
    public String toString() {
        return "XMXliffStringModel [xliffTestModel=" + xliffTestModel + "]";
    }

}

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;

@Root(name = "string")
public class XliffTestModel {

    @Attribute(required = false, name = "name")
    public String attname = "";

    @Namespace(prefix = "xliff")
    @Element(required = false, name = "g")
    public String g;

    @Override
    public String toString() {
        return "XliffTestModel [attname=" + attname + ", g=" + g + "]";
    }

}

[主要代码]

XMXliffStringModel xmXliffStringModel = new XMXliffStringModel();
String path = "C:/Users/SEC/Desktop/strings.xml";
Serializer serializer = new Persister();
try {
    xmXliffStringModel = serializer.read(xmXliffStringModel.getClass(), new File(path));
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println(xmXliffStringModel);

1 个答案:

答案 0 :(得分:0)

内部文本有点棘手,因为Simple不允许将text和(child-)元素放在一起。但请在此处查看我的答案,如何解决这个问题:

https://stackoverflow.com/questions/17462970/simpleframwork-xml-element-with-inner-text-and-child-elements/17496648#17496648

关键部分是QuestionConvert类。