相关疑难解决方法(0)

JAXB:如何将地图编组为<key> value </ key>

问题是关于JAXB Map编组 - 有很多关于如何将Map转换为如下结构的示例:

<map>
  <entry>
    <key> KEY </key>
    <value> VALUE </value>
  </entry>
  <entry>
    <key> KEY2 </key>
    <value> VALUE2 </value>
  </entry>
  <entry>
  ...
</map>
Run Code Online (Sandbox Code Playgroud)

实际上,这是JAXB原生支持的.但是,我需要的是XML,其中key是元素名称,value是其内容:

<map>
  <key> VALUE </key>
  <key2> VALUE2 </key2>
 ...
</map>
Run Code Online (Sandbox Code Playgroud)

我没有按照JAXB开发人员推荐的方式(https://jaxb.dev.java.net/guide/Mapping_your_favorite_class.html)成功实现我的Map适配器,因为我需要,他 - 动态属性名称:)

那有什么解决方案吗?

PS目前我必须为每个我想要编组的典型键值对创建一个专用的容器类 - 它可以工作,但是我必须创建太多这些辅助容器.

java xml jaxb jaxb2

64
推荐指数
4
解决办法
10万
查看次数

如何将字符串xml转换为Map <String,String>

如何将xml的元素和属性的所有值转换为字符串的Map?有些图书馆可以做到这一点吗?我找到了库xStream,但我不知道如何配置它.

java xml map

2
推荐指数
1
解决办法
2万
查看次数

如何使用XStream解组Map

我似乎无法使用XStream正确地解组Map.

我有以下代码:

存储类

public class Storage
{

    @XStreamAsAttribute
    private String basedir;

    @XStreamAsAttribute
    private Map<String, Repository> repositories = new LinkedHashMap<String, Repository>();

...

}
Run Code Online (Sandbox Code Playgroud)

存储库类

public class Repository
{

    @XStreamAsAttribute
    private String name;

    @XStreamAsAttribute
    private String basedir;

    @XStreamAsAttribute
    private int policy;

    @XStreamAsAttribute
    private int layout;

    @XStreamAsAttribute
    private int type;

...

}
Run Code Online (Sandbox Code Playgroud)

ConfigurationParser类

public class ConfigurationParser
{

    public Storage parseConfiguration(String xmlFile)
            throws FileNotFoundException
    {
        FileInputStream fis = new FileInputStream(xmlFile);

        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);

        xstream.alias("storage", Storage.class);
        xstream.alias("repositories", Map.class);
        xstream.alias("repository", Repository.class);

        return (Storage) …
Run Code Online (Sandbox Code Playgroud)

java xstream maven

1
推荐指数
1
解决办法
9845
查看次数

将XML String转换为Map并使用Java获取键和值对

我有一个XML字符串.我正在尝试将该字符串转换为地图,以便我可以获得键和值.但它无法转换.这是我的代码

String xmlString = "<?xml version="1.0" encoding="UTF-8"?><user>
                        <kyc></kyc>
                        <address></address>
                        <resiFI></resiFI></user>"

def convertStringToDocument = {
        xmlString ->
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder;
            try {
                builder = factory.newDocumentBuilder();
                org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
                return doc;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
    }
    def populateDocProofsFromWaiversXML = {
        xmlString, mandateFlag ->

            final List<DocumentProof> documentProofs = new ArrayList<DocumentProof>();
            if (xmlString != null) {
                try {
                    HashMap<String, String> values = new HashMap<String, String>();
                    Document xml = convertStringToDocument(waiversList);
                    org.w3c.dom.Node user = xml.getFirstChild(); …
Run Code Online (Sandbox Code Playgroud)

java xml dictionary xstream

0
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×4

xml ×3

xstream ×2

dictionary ×1

jaxb ×1

jaxb2 ×1

map ×1

maven ×1