如何将字符串xml中的字符串xml解析/解组为Java对象? [英] How to parse/unmarshall the string xml inside a string xml into a Java object?

查看:157
本文介绍了如何将字符串xml中的字符串xml解析/解组为Java对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,正在解析/将字符串xml解析为Java对象.我只想知道如何在字符串xml中获取字符串xml并将其转换为java对象.

Im newbie on parsing/umarshalling string xml to java object. I just want to know how to get the string xml inside a string xml and convert to java object.

这是我从HTTP GET获得的字符串xml:

Here is my string xml from a HTTP GET:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">&lt;?xml version="1.0" encoding="utf-8" ?    
&gt;&lt;MyList&gt;&lt;Obj&gt;Im obj 1&lt;/Obj&gt;&lt;Obj&gt;Im obj       
1&lt;/Obj&gt;&lt;/MyList&gt;</string>

我注意到stackoverflow正在删除根元素(即字符串")并仅显示
<?xml version ="1.0" encoding ="utf-8"吗?> < MyList> < Obj> Im obj 1</Obj> < Obj> Im obj 2</Obj> </MyList> 如果我没有将此字符串xml放在代码块中,则立即进行.

I notice stackoverflow is removing the root element which is the "string" and just displaying
<?xml version="1.0" encoding="utf-8" ?> <MyList> <Obj>Im obj 1</Obj> <Obj>Im obj 2</Obj> </MyList> right away if i didn't put this string xml inside a code block.

我正在尝试使用JDom 2,但没有运气.它只会获取根元素,而不会获取子元素.

I'm trying to use JDom 2 but no luck. It only get the root element but not the children.

我还使用了JAXB:

我可以获取根元素,但不能获取子元素.这是我的代码:

I can get the root element but not the children. Here is my code:

JAXBContext jc = JAXBContext.newInstance(myPackage.String.class);           
Unmarshaller unmarshaller = jc.createUnmarshaller(); 
JAXBElement<MyList> jaxbElement = unmarshaller.unmarshal(new StreamSource(new   
ByteArrayInputStream(byteArray)), MyList.class); 

System.out.println(jaxbElement.getClass()); --> this will print myPackage.MyList                                                  

MyList myList = (MyList) jaxbElement.getValue();
System.out.println("myList.Obj = " + myList.getObjs().size()); --> this will return 0

推荐答案

我刚刚使用了JAXB变体:

I just got a JAXB variant working:

public class XmlParser
{
    @XmlRootElement( name = "string", namespace = "http://tempuri.org/" )
    @XmlAccessorType( XmlAccessType.FIELD )
    static class MyString
    {
        @XmlValue
        String string;
    }

    @XmlRootElement( name = "MyList" )
    @XmlAccessorType( XmlAccessType.FIELD )
    static class MyList
    {
        @XmlElement( name = "Obj" )
        List<String> objs = new ArrayList<>();
    }

    public static void main( String[] args ) throws JAXBException
    {
        String s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                + "<string xmlns=\"http://tempuri.org/\">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;&lt;MyList&gt;&lt;Obj&gt;Im obj 1&lt;/Obj&gt;&lt;Obj&gt;Im obj1&lt;/Obj&gt;&lt;/MyList&gt;</string>";

        JAXBContext context = JAXBContext.newInstance( MyString.class, MyList.class );
        Unmarshaller unmarshaller = context.createUnmarshaller();

        MyString myString = (MyString) unmarshaller.unmarshal( new StringReader( s ) );
        MyList myList = (MyList) unmarshaller.unmarshal( new StringReader( myString.string ) );

        System.out.println( myList.objs );
    }
}

这篇关于如何将字符串xml中的字符串xml解析/解组为Java对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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