SteBu (2) [Avatar] Offline
#1
Hi, I'm a student and for a study I'm trying to get an XML of a Java-Object, but I cannot find the solution. Neither in the book nor in the web.

I checked nearly all tag references but it seems to me, that there is no solution for my problem.

Then I tried to generate my own subtask and template but this "experiment" failed. Perhaps here has someone a solution or an idea?

Thanks in Advance
habuma (279) [Avatar] Offline
#2
Re: Getting XML of an Object
What exactly is it that you're trying to accomplish? It sounds to me like you're trying to serialize a Java object into XML.

If that's the case, then XDoclet is not the tool you want. You should look into something like Castor XML (that said, XDoclet can help you generate your Castor descriptor files) or XMLBeans.

If that's not what you're trying to accomplish, then please state (with examples) what it is you need to do and I'll try to help.
SteBu (2) [Avatar] Offline
#3
Re: Getting XML of an Object
Hi Habuma,

first, thanks for your answer.

Now to what I want to do. I want Xdoclet to craete an instance of a class, which has a special annotation, and than to create XML-file of an attribute of the class.

But it sounds as if it is impossible to realise this with xdoclet.
habuma (279) [Avatar] Offline
#4
Re: Getting XML of an Object
I'm sorry, but I'm still a bit confused as to what your goals are. Let me offer a few scenarios and you tell me which one is most like what you are trying to do:

1) Let's say you have an instance of Employee named emp such that:

emp.getId() = 987;
emp.getName() = "Bob Smith";
emp.getPhone() = "5551212";

Do you want an XML file that looks something like this:

<employee id="987">
<name>Bob Smith</name>
<phone>5551212</phone>
</employee>

This is an example of serializing an object into XML. XDoclet cannot do this. Other tools such as CastorXML and XMLBeans do this. The difference is that XDoclet is a code-generation engine that works with a class definition as its source, whereas object serialization works with a class instance.

2) You have a class that looks like this:

public class Employee {
private int id;
private String name;

public void setId(int id) {this.id = id;}
public int getId() {return id};

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

Do you want an XML file something like this:

<class name="Employee">
<id name="id" type="int"/>
<property name="name" type="java.lang.String"/>
</class>

In this case, an XML file is generated based on the class definition (not a class instance). This is something that XDoclet can help you with. If this is what you want, tell me a bit more about what the type of class and the structure of the XML you want to generate and I'll try to help out.