2

enter code hereI've to deserialize this XML:

<rows profile="color">
    <head>
        <columns>
            <column width="0" align="left" type="ro" sort="str" color=""><![CDATA[#]]></column>
            <column width="80" align="left" type="ro" sort="str" color=""><![CDATA[Targa]]></column>
            <column width="100" align="left" type="ro" sort="str" color=""><![CDATA[Telaio]]></column>
            <column width="150" align="left" type="ro" sort="str" color=""><![CDATA[Tipo]]></column>
            <column width="70" align="left" type="ro" sort="str" color=""><![CDATA[Archivio]]></column>
            <column width="220" align="left" type="co" sort="str" color=""><![CDATA[Commenti]]><option value="A">A</option><option value="B">B</option><option value="C">C</option></column>
            <column width="180" align="left" type="ed" sort="str" color=""><![CDATA[Destinatario]]></column>
        </columns>
    </head>
    <row>
        <cell><![CDATA[775]]></cell>
        <cell><![CDATA[AA000AA]]></cell>
        <cell><![CDATA[RTGGSHHJSJSNN]]></cell>
        <cell><![CDATA[CDP]]></cell>
        <cell><![CDATA[18]]></cell>
        <cell><![CDATA[...]]></cell>
        <cell><![CDATA[ ]]></cell>
    </row>
</rows>

But I haven't a defined class, how can I do it? I would use xstream library, but I don't know how use it.

EDIT:

But if I want to create a destination class, how I create it? I should have something like:

public class Rows { 
    private Head head; 
    private Row[] row; 
}

public class Head { 
    private Columns columns;
} 

public class Columns {
    private Column column; // How can I get attributes?
}

public class Row {
    private String [] cell;
}

and how can I use xstream after?

3
  • 1
    You haven't a defined class - meaning, you don't have classes corresponding to the XML structure (e.g Rows, Head, Cell etc.)? What type of object do you want the deserialization to return? Commented Dec 28, 2011 at 14:44
  • Exactly. I don't know, there is a method to do it without a class? Commented Dec 28, 2011 at 14:45
  • Agreed to @EliAcherkan. If you want to deserialize an object, you need to create and work with a class as long, as it finally deserializes properly from provided input. Commented Dec 28, 2011 at 14:47

3 Answers 3

2

Take a look on JAXB. ( http://jaxb.java.net/ )

It's a great lib for read/write a XML into/from classes. I know there are some plugins for it. I'm almost sure you can generate/create classes from a XML file or create the XML from annotaded classes.

Some examples: http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/tutorial/doc/JAXBUsing3.html

Hello World: http://jaxb.java.net/tutorial/section_1_3-Hello-World.html

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed. Jaxb is great project, although sometimes the amount of XML configuration does terrify me ;)If you want to deserialize an object to work with it, you should give it a go. Other responses are suggesting using SAX parser, but this it not what you asked for. SAX parser could be helpful to read huge amount of XML, analyze the data stored with it and maybe fetch some of it. But it won't give you deserialized object - period.
0

Since you do not have a target class to which XStream can deserialize, you will have to use other XML parsers.

You can take a look at StAX. You can find how to use it here and here.

Comments

0

Use SAX

SAX parser is work differently with DOM parser, it either load any XML document into memory nor create any object representation of the XML document. Instead, the SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure.

if you want create a class from XML use Digester

Example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.