当前位置: 首页 > 知识库问答 >
问题:

XStream上用于XML转换的等效DROP_ROOT_MODE

蒙洛华
2023-03-14
public static String entityToXML(GenericResponseObject entity) {
    XStream xstream = new XStream(new StaxDriver());

    xstream.autodetectAnnotations(true);
    xstream.registerConverter(new GenericResponseAttributeConverter());
    String xml = xstream.toXML(entity);

    return xml;
}
@XStreamAlias("objectResult")
public class GenericResponseObject {

    @XStreamAlias("attributes")
    @XStreamImplicit
    private ArrayList<GenericResponseAttribute> attributes;

    public GenericResponseObject() {
        this.attributes = new ArrayList();
    }

    public void addAttribute(String name, Object value) {
        this.attributes.add(new GenericResponseAttribute(name, value));
    }
}

类GenericResponseAttribute:

@XStreamAlias("attribute")
public class GenericResponseAttribute {

    private String name;
    private Object value;

    public GenericResponseAttribute(String name, Object value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getValue() {
        return this.value;
    }

    public void setValue(Object value) {
        this.value = value;
    }
}

正如您所读到的,每个类都有用于别名和隐式列表的XStream注释,因此,让我向您展示我为GenericResponseAttribute类制作的自定义转换器:

public class GenericResponseAttributeConverter implements Converter {

    @Override
    public boolean canConvert(Class type) {
        return type.equals(GenericResponseAttribute.class);
    }

    @Override
    public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
        GenericResponseAttribute value = (GenericResponseAttribute)o;
        writer.startNode(value.getName());
        mc.convertAnother(value.getValue());
        writer.endNode();
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

因此,如果我在运行时构建一个GenericResponseObject,并用静态方法将其解析为XML,K将得到如下内容:

response = new GenericResponseObject();
response.addAttribute("user", "alex");
response.addAttribute("otherAtt", "TEST");
System.out.println(XMLUtil.entityToXML(response));
<objectResult>
    <attribute>
        <user>hugo</user>
    </attribute>
    <attribute>
        <otherAtt>TEST</otherAtt>
    </attribute>
</objectResult>
<objectResult>
    <user>hugo</user>
    <otherAtt>TEST</otherAtt>
</objectResult>

我的需求是非常基本的,但我不知道如何满足它,我搜索了一下,在HierarchicalStreamWriter类中似乎没有像deletRootNode()或replaceRootNode()这样的方法,而且在GenericResponseAttribute中也没有可以使用的@XStreamNoOot或@XStreamMakeRoot注释,所以,这就是为什么我在这里询问的原因,如果您知道如何做到这一点,请帮助我。

多谢了。

共有1个答案

水渊
2023-03-14

当我在将对象解析为XML后使用字符串替换时,我的意思是:

public static String entityToXML(Object entity) {
    XStream xstream = new XStream(new StaxDriver());

    xstream.autodetectAnnotations(true);
    xstream.registerConverter(new GenericResponseAttributeConverter());
    String xml = xstream.toXML(entity);

    xml = xml.replace("<attribute>", "");
    xml = xml.replace("</attribute>", "");

    return xml;
}

但是,这不是一个很酷的解决方案,如果你有另一个请告诉我。

多谢了。

 类似资料:
  • 问题内容: 有没有办法从XML打开Spring的Async配置?我看到的所有示例都使用程序化上下文声明和使用 是否有与此等效的XML。在某些地方,我看到有人在使用它,但这并未提及async。 我正在使用Spring 4。 问题答案: 你试过用这个吗

  • 问题内容: 我最近使用的Train status API在JSON对象中添加了两个额外的键值对,这导致我的脚本崩溃。 这是字典: 毫不奇怪,我得到了以下错误: 如果我没记错的话,我认为这是因为JSON响应中的布尔值是/,而Python可以识别/ 。有什么办法解决吗? PS:我尝试将的JSON响应转换为字符串,然后将其转换回布尔值,结果发现如果字符串中有任何字符,我总会得到一个值。我有点卡在这里。

  • 问题内容: 我让jQuery提取一些textarea内容并将其插入li中。 我希望它在视觉上保留换行符。 必须有一种非常简单的方法来执行此操作… 问题答案:

  • 问题内容: 我正在寻找Linux上的等效工具。 目前,我正在使用Python的,它大概会调用。我担心的是,如果时钟混乱,例如NTP,返回的时间(UNIX纪元)可能会发生不规则的变化。一个简单的过程或系统挂墙时间,仅以恒定的速率正增加就足够了。 在C或Python中是否存在任何此类时间函数? 问题答案: 您可以在C中使用CLOCK_MONOTONIC,例如:

  • 如何编写java类来读取此XMl文件 我试过这样 但我得到了这个错误 请帮我解决这个问题。。。 非常感谢。

  • XML输入文件: XML输入文件转换为XML输出文件。将XML输入文件的firstName、middleName和lastName标签合并为XML Ouput文件的name标签,将XML输入文件的address1、address2、city、state和pincode标签合并为XML Ouput文件的address标签。 我几乎转换了代码,但我在这里与empId作斗争。我已经在XSLT文件中手动输