1 What I need i" /> 1 What I need i" /> 1 What I need i"/>

Castor read an attribute of a text element

98 Views Asked by At

I would like to know how to map this using castor mappings (version 0.9.9.1).

<Classifica>
    <Livello nome="812">1</Livello>
</Classifica>

What I need it the value of the nome attribute.

I've tried this:

<class name="it.ClassificaIDT" auto-complete="true">
    <map-to xml="Classifica"/>
    <field name="livello"
           type="it.LivelloIDT"
           container="false">
      <bind-xml name="Livello"/>
    </field>
  </class>
  <class name="it.LivelloIDT" auto-complete="true">
    <map-to xml="Livello"/>
    <field name="nome">
      <bind-xml name="nome" node="attribute"/>
    </field>
  </class>

My class is:

package it;

public class ClassificaIDT {
    private LivelloIDT livello;

    public LivelloIDT getLivello() {
        return livello;
    }

    public void setLivello(LivelloIDT livello) {
        this.livello = livello;
    }
}

The above gives this error:

Caused by: Illegal Text data found as child of: Livello
  value: "1"{file: [not available]; line: 42; column: 56}
    at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:671)
    at it.bz.prov.egov.foundation.fascicoloinformatico.service.eprocs.XMLConverter.createOutputForEProcsWebService(XMLConverter.java:121)
    ... 148 more
Caused by: org.xml.sax.SAXException: Illegal Text data found as child of: Livello
  value: "1"
    at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:889)

How can I ignore the value '1'? Or even better, how to map it to a class property ?

Thank you.

1

There are 1 best solutions below

0
Felix Sima On

I've found the solution:

Mapping:

<class name="it.LivelloIDT" auto-complete="true">
    <map-to xml="Livello"/>
    <field name="nome" type="java.lang.String" get-method="getNome" set-method="setNome">
      <bind-xml name="nome" node="attribute"/>
    </field>
    <field name="text" type="java.lang.String">
      <bind-xml node="text" />
    </field>   </class>

Class:

public class LivelloIDT {
    private String nome;
    private String text;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

Note the text field