gpt4 book ai didi

java - XStream 双下划线处理 (Java)

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:23:38 24 4
gpt4 key购买 nike

我想使用 XStream 将 java 类序列化为 XML。

我需要生成一个带有一个下划线 (canal_operateur) 的 XML 元素,但 XStream 在 xml 元素名称中添加了第二个下划线。

那么如何只保留一条下划线呢?

使用这个类的默认配置:

public class Document implements Serializable {
private String typedoc;
private Fichier fichier;
private String canalOperateur;
//xstream.aliasField("canal_operateur", Document.class, "canalOperateur");
/*
** setter and getter
*/
}

XStream 加倍下划线:

<document>
<canal__operateur>canal</canal__operateur>
<commentaire>commentaire</commentaire>
<metadonnees/>
</document>

最佳答案

根据 here :

XStream maps Java class names and field names to XML tags or attributes. Unfortunately this mapping cannot be 1:1, since some characters used for identifiers in Java are invalid in XML names. Therefore XStream uses an XmlFriendlyNameCoder to replace these characters with a replacement. By default this NameCoder uses an underscore as escape character and has therefore to escape the underscore itself also. You may provide a different configured instance of the XmlFriendlyNameCoder or a complete different implementation like the NoNameCoder to prevent name coding at all. However it is your responsibility then to ensure, that the resulting names are valid for XML.

关于java - XStream 双下划线处理 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9800494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com