简体   繁体   English

如何将 xml 数据提取到 Map 的字符串列表中?

[英]How to extract xml data to a list of Map of Strings?

I have an xml data like this我有这样的 xml 数据

  `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <books>
     <book>
       <name>SCJP</name>
       <author>Kathy Sierra</author>
       <publisher>Tata McGraw Hill</publisher>
       <isbn>856-545456736</isbn>
     </book>
     <book>
       <name>Java Persistence with Hibernate</name>
       <author>Christian Bauer</author>
       <publisher>Manning</publisher>
       <isbn>978-3832180577</isbn>
     </book>
   </books>`

Is there a way to to convert this xml data to List<Map<String,String>> or List<Map<String,Object>> with out using a class?有没有办法在不使用 class 的情况下将此 xml 数据转换为 List<Map<String,String>> 或 List<Map<String,Object>>? .Please help. 。请帮忙。 Thanks谢谢

I have tried to extract this with java code using JAX unmarshalling,我尝试使用 JAX 解组用 java 代码提取它,

https://ibytecode.com/blog/jaxb-marshalling-and-unmarshalling-list-of-objects/ , https://ibytecode.com/blog/jaxb-marshalling-and-unmarshalling-list-of-objects/ ,

which is working for me and i am getting results like这对我有用,我得到的结果就像

  [Book [name=SCJP, author=Kathy Sierra, publisher=Tata McGraw Hill, isbn=856-545456736], 
  Book [name=Java Persistence with Hibernate, author=Christian Bauer, publisher=Manning, isbn=978-  3832180577]]

I am only expecting我只是期待

[{name="SCJP", author="Kathy Sierra", publisher="Tata McGraw Hill", isbn="856-545456736"}, {name="Java Persistence with Hibernate", author="Christian Bauer", publisher="Manning", isbn="978- 3832180577"}] [{name="SCJP", author="Kathy Sierra", publisher="Tata McGraw Hill", isbn="856-545456736"}, {name="Java Persistence with Hibernate", author="Christian Bauer", publisher ="曼宁", isbn="978- 3832180577"}]

In XQuery 3.1, using the Saxon API, run the query在 XQuery 3.1 中,使用 Saxon API,运行查询

/books/book ! map:merge(* ! map{local-name(.) : string(.)})

This will return an XdmValue which can be processed directly in your Java application, or converted to the form you require.这将返回一个XdmValue ,它可以在您的 Java 应用程序中直接处理,或转换为您需要的形式。 To convert it to a List<Map<String, String>> you could use要将其转换为List<Map<String, String>>您可以使用

List<Map<String, String> list = new ArrayList<>();
for (XdmItem item : xdmValue) {
  XdmMap xMap = (XdmMap)item;
  Map<String, String> jMap = new HashMap<>();
  for (XdmAtomicValue key : xMap.keySet()) {
     jMap.put(key.getStringValue(),
              ((XdmAtomicValue)xMap.get(key)).getStringValue();
  }
  list.add(jMap);
}

In my view this kind of approach is much more flexible than anything using JAXB, because your application does not have any tight coupling to the XML schema for the data.在我看来,这种方法比使用 JAXB 的任何方法都灵活得多,因为您的应用程序与数据的 XML 模式没有任何紧密耦合。

You could of course use a different XQuery 3.1 processor such as BaseX, but the API would be slightly different.您当然可以使用不同的 XQuery 3.1 处理器,例如 BaseX,但 API 会略有不同。

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

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