定义命名空间标签以便生成的 XML 具有这些标签? [英] Define namespaces tags so that generated XML have those tags?

查看:37
本文介绍了定义命名空间标签以便生成的 XML 具有这些标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类 People.java 和 PeopleMain.java

I have two classes People.java and PeopleMain.java

People.java

People.java

package com.test;

public class People {

    private String name;
    private String age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }   

}

PeopleMain.java

PeopleMain.java

package com.test;

import com.thoughtworks.xstream.XStream;

public class PeopleMain {

    public static void main(String args[]){

        People p= new People();

        p.setAge("21");
        p.setName("Manish Sharma");

        XStream xs =new XStream();

        String xml = xs.toXML(p);

        System.out.println(xml);    
    }
}

我在控制台上运行 PeopleMain.java 的输出如下:

My output on console on running PeopleMain.java comes as:

<com.test.People>
  <name>Manish Sharma</name>
  <age>21</age>
</com.test.People>

但我想要一个输出

<People xmlns:ns2="http://example.com/foo" xmlns:ns3="http://example.com/bar">
  <ns2:name>Manish Sharma</ns2:name>
  <ns3:age>21</ns3:age>
</People>

我应该在 People.java 文件中进行哪些更改以获得所需的输出?

What changes should I make in my People.java file to get the desired output?

推荐答案

不幸的是,根据 XSTream 常见问题,XStream 不支持 XML 命名空间,除非使用 StAX 解析器.

Unfortunately, per the XSTream FAQ, XStream does not support XML namespaces unless using a StAX parser.

不是每个 XML 解析器都支持命名空间,也不是每个 XML 解析器都支持命名空间支持命名空间可以在 XStream 中配置以使用它们.基本上命名空间必须单独支持不同的XML 解析器和目前唯一支持的命名空间在 XStream 中实现的是 StAX 传递器.因此使用和配置 XStream 的 StaxDriver 以使用命名空间.

Not every XML parser supports namespaces and not every XML parser that supports namespaces can be configured within XStream to use those. Basically namespaces must be supported individually for the different XML parsers and the only support for namespaces that has currently been implemented in XStream is for the StAX paser. Therefore use and configure the StaxDriver of XStream to use namespaces.

这篇关于定义命名空间标签以便生成的 XML 具有这些标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆