尝试使用地图中的属性和地图键值解析 XML

Trying to parse XML with attribute and map key values in a Map

我必须解析一个 XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <contentType>Document</contentType>
    <siteName>mySite</siteName>
    <listName>myLib</listName>
    <folderName>docset-folder</folderName>
    <documentSetName>documentSet</documentSetName>
    <fields>
        <field name="META_1">123456789</field>
        <field name="META_2">Someone</field>
        <field name="META_3">Germany</field>
        <field name="META_4">Abonnement2 LC</field>
    </fields>
</data>

我创建了一个简单的 POJO class 来映射这个 XML。我设置了 XML 文件的属性。 关于该字段,我可以拥有尽可能多的具有唯一名称的数据。

@Data
@XmlRootElement(name = "data")
public class MetaDataSharePoint {
    @JacksonXmlProperty(localName = "contentType")
    private String contentType;
    @JacksonXmlProperty
    private String siteName;
    @JacksonXmlProperty
    private String listName;
    @JacksonXmlProperty
    private String folderName;
    @JacksonXmlProperty
    private String documentSetName;
    @JacksonXmlElementWrapper(localName = "fields")
    @JacksonXmlProperty(localName = "field")
    private Map<String, Object> fields;
}

我的 objective 是正确映射这个 XML 并处理一个 POJO。以下代码允许打开 XML 文件并读取其内容。 XMLMapper 对象允许匹配 xml 和对象。

public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, XMLStreamException, TransformerException {
    String path = "src/test/resources/bill.xml";
    XMLInputFactory f = XMLInputFactory.newFactory();
    XMLStreamReader sr = f.createXMLStreamReader(new FileInputStream(path));
    XmlMapper mapper = new XmlMapper();

    sr.next();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    MetaDataSharePoint metaDataSharePoint = mapper.readValue(sr, MetaDataSharePoint.class);

    // start test
    System.out.println(metaDataSharePoint);
    System.out.println("contentType : " + metaDataSharePoint.getContentType());
    System.out.println("DocumentSetName : " + metaDataSharePoint.getDocumentSetName());
    System.out.println("ListName : " + metaDataSharePoint.getListName());
    System.out.println("SiteName : " + metaDataSharePoint.getSiteName());
    System.out.println("FolderName : " + metaDataSharePoint.getFolderName());
    metaDataSharePoint.getFields().forEach((key, value) -> {
        System.out.println(key);
        System.out.println(value);
    });
    // end test

    sr.close();
}

我成功映射了部分对象。 我打印以下内容:

MetaDataSharePoint(contentType=Document, siteName=mySite, listName=myLib, folderName=docset-folder, documentSetName=documentSet, fields={field={name=META_4, =Abonnement2 LC}})
contentType : Document
DocumentSetName : documentSet
ListName : myLib
SiteName : mySite
FolderName : docset-folder
field
{name=META_4, =Abonnement2 LC}

我的地图没有返回预期的结果。我想添加到地图键和值。 我希望是这样的:

key: META_1
value: 123456789

key: META_2
value: Someone

...

我尝试使用多种方法来映射此 XML,但均未成功。 谢谢

我简单修改了我的 POJO :

@Data
@AllArgsConstructor @NoArgsConstructor
@Builder
@XmlRootElement(name = "data")
public class MetaDataSharePoint {
    @JacksonXmlProperty(localName = "contentType")
    private String contentType;
    @JacksonXmlProperty
    private String siteName;
    @JacksonXmlProperty
    private String listName;
    @JacksonXmlProperty
    private String folderName;
    @JacksonXmlProperty
    private String documentSetName;
    @JacksonXmlProperty
    private List<MetaDataFields> fields;
}

我用对象列表替换了地图。为此,我实现了这个 POJO :

@Data
@Builder
@AllArgsConstructor @NoArgsConstructor
@XmlRootElement(name = "field")
public class MetaDataFields {
    @JacksonXmlProperty(isAttribute = true)
    private String name;
    @JacksonXmlText
    private String value;
}

我按预期检索了数据。太棒了...