Jackson的Java XML解析错误

时间:2014-10-10 13:51:28

标签: java xml parsing serialization jackson

我正在尝试转换此xml:

<resources>
     <string name="string_one">one</string>
     <string name="string_two">two</string>
     <string name="test2">test2</string>
          <plurals name="plurals_one">
            <item quantity="one">one</item>
            <item quantity="other">other</item>
          </plurals>
     <string name="test1">test1</string>
</resources>

这样的事情:

AndroidTextResource{test1, test1}
AndroidTextResource{plurals_one@PLURAL@one, one}
AndroidTextResource{plurals_one@PLURAL@other, other}
AndroidTextResource{string_one, one}
AndroidTextResource{string_two, two}

问题是它不能解析“复数”标记之后的任何内容。 这就是我得到的:

AndroidTextResource{test1, test1}
AndroidTextResource{plurals_one@PLURAL@one, one}
AndroidTextResource{plurals_one@PLURAL@other, other}

这是我的代码:

@JacksonXmlRootElement(localName="resources")
public class AndroidTextResources {

@JsonPropertyOrder({"key", "value"})
public static class MyClass {
    @JacksonXmlProperty(isAttribute=true, localName="name")
    public String key;
    @JacksonXmlText
    public String value;

    public AndroidTextResource() {
    }

    public AndroidTextResource(final String key, final String value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(
                key, 
                value);
    }

    @Override
    public boolean equals(final Object other) {
        if (other == this) { return true; }
        if (other == null) { return false; }
        if (! (other instanceof AndroidTextResource)) { return false; }
        final AndroidTextResource that = (AndroidTextResource)other;

        return
        equal(this.key,   that.key) &&
        equal(this.value, that.value)
        ;
    }

    @Override 
    public String toString() {
        return Objects.toStringHelper(getClass().getSimpleName())
                .addValue(key)
                .addValue(value)
                .toString();
    }
}

public static class AndroidPlural {
    public static class AndroidPluralItem {
        @JacksonXmlProperty(isAttribute=true)
        public String quantity;
        @JacksonXmlText
        public String value;

        public AndroidPluralItem() {}

        public AndroidPluralItem(final String quantity, final String value) {
            this.quantity = quantity;
            this.value = value;
        }
    }

    @JacksonXmlProperty(isAttribute=true, localName="name")
    public String key;

    @JacksonXmlElementWrapper(useWrapping=false)
    @JacksonXmlProperty(isAttribute=false, localName="item")
    public List<AndroidPluralItem> items = new ArrayList<MyClass.AndroidPlural.AndroidPluralItem>();

    public AndroidPlural() {}

    public AndroidPlural(final String key) {
        this.key = key;
    }

    public List<AndroidTextResource> toTextResources() {
        final List<AndroidTextResource> result = new ArrayList<MyClass.AndroidTextResource>(items.size());
        for (final AndroidPluralItem item : items) {
            final AndroidTextResource res = new AndroidTextResource();
            res.key = key + "@PLURAL@" + item.quantity;
            res.value = item.value;
            result.add(res);
        }
        return result;
    }
 }

@JacksonXmlElementWrapper(useWrapping=false)
@JacksonXmlProperty(isAttribute=false, localName="string")
public List<AndroidTextResource> textResources = new ArrayList<MyClass.AndroidTextResource>();

@JacksonXmlElementWrapper(useWrapping=false)
@JacksonXmlProperty(isAttribute=false, localName="plurals")
public List<AndroidPlural> plurals = new ArrayList<MyClass.AndroidPlural>();

}


public class AndroidResourceToCsv {

public static void main(final String[] args) throws Exception {
    if (args.length != 2) {
        System.out
                .println("You need to specify exactly one input and one output file.");
        System.exit(-1);
    }
    final File inputFile = new File(args[0]);
    final File outputFile = new File(args[1]);

    InputStream inputStream = null;

    try {
        inputStream = new FileInputStream(inputFile);
    } finally { inputStream.close(); }

    final List<AndroidTextResource> allResources = readCsv(inputStream);

    outputFile.getParentFile().mkdirs();

    final CsvMapper csvMapper = new CsvMapper();
    final CsvSchema schema = csvMapper.schemaFor(AndroidTextResource.class)
            .withColumnSeparator(';');
    final ObjectWriter writer = csvMapper.writer().withSchema(schema);
    writer.writeValue(outputFile, allResources);

    // TODO: parse three times, for string, plural & string array, ignore
    // other values
}

public static List<AndroidTextResource> readCsv(final InputStream in)
        throws JsonParseException, JsonMappingException, IOException {

    final ObjectMapper xmlMapper = new XmlMapper();
    final MyClass = xmlMapper.readValue(in,
            MyClass.class);

    final List<AndroidTextResource> allResources = new ArrayList<MyClass.AndroidTextResource>();
    allResources.addAll(resources.textResources);

    for (final AndroidPlural plural : resources.plurals) {
        allResources.addAll(plural.toTextResources());
    }

    return allResources;

}
}

有谁知道出了什么问题?提前致谢!

0 个答案:

没有答案