简体   繁体   中英

How can I extract content between two XML tags as text using Jackson?

I have an XML with the following template:

<tag1 attr1="value1" attr2="value2">
    <tag2> text </tag2>
    <tag3> another text </tag3>
</tag1>

I want to extract this xml into a POJO that has 2 text fields as String and 2 fields for attributes, but I don't quite understand how to use JacksonXmlText .

First, start from some tutorials. For example, please, take a look on this page: Convert XML to JSON Using Jackson .

Some key points:

  1. Use com.fasterxml.jackson.dataformat.xml.XmlMapper to serialize / deserialize XML
  2. Use annotation from this package com.fasterxml.jackson.dataformat.xml.annotation

Your code could look like below:

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

public class Main {

    public static void main(String[] args) throws Exception {
        XmlMapper mapper = new XmlMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        Tag1 tag1 = new Tag1();
        tag1.setTag2("text");
        tag1.setTag3("another text");
        tag1.setAttribute1("value1");
        tag1.setAttribute2("value2");
        String xml = mapper.writeValueAsString(tag1);
        System.out.println(xml);

        System.out.println(mapper.readValue(xml, Tag1.class));
    }
}

@JacksonXmlRootElement(localName = "tag1")
class Tag1 {

    private String attribute1;
    private String attribute2;
    private String tag2;
    private String tag3;

    @JacksonXmlProperty(isAttribute = true)
    public String getAttribute1() {
        return attribute1;
    }

    public void setAttribute1(String attribute1) {
        this.attribute1 = attribute1;
    }

    @JacksonXmlProperty(isAttribute = true)
    public String getAttribute2() {
        return attribute2;
    }

    public void setAttribute2(String attribute2) {
        this.attribute2 = attribute2;
    }

    public String getTag2() {
        return tag2;
    }

    public void setTag2(String tag2) {
        this.tag2 = tag2;
    }

    public String getTag3() {
        return tag3;
    }

    public void setTag3(String tag3) {
        this.tag3 = tag3;
    }

    @Override
    public String toString() {
        return "Tag1{" +
                "attribute1='" + attribute1 + '\'' +
                ", attribute2='" + attribute2 + '\'' +
                ", tag2='" + tag2 + '\'' +
                ", tag3='" + tag3 + '\'' +
                '}';
    }
}

Above code prints:

<tag1 attribute1="value1" attribute2="value2">
  <tag2>text</tag2>
  <tag3>another text</tag3>
</tag1>

Tag1{attribute1='value1', attribute2='value2', tag2='text', tag3='another text'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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