Frage

I'm parsing some QTI test and questions in Java with SimpleFramework. I have already parsed a lot of type of questions but I'm kind of stuck here. I don't know how I can parse this.

I don't know how I can deal with this text without tag. The parsing works but how can I reach this text ?

<p>
   Now is the 
   <gap identifier="gap1"/>
   of our discontent<br/> Made glorious 
   <gap identifier="gap2"/>
   by this sun of York;
   <br/> And all the clouds that lour'd upon our house
   <br/> In the deep bosom of the ocean buried.
</p>

I got this

public class P extends AtomicBlock {}

public abstract class AtomicBlock extends BlockStatic {

@ElementListUnion({
        @ElementList(entry = "gap", type = Gap.class, inline = true, required = false),
        @ElementList(entry = "br", type = Br.class, inline = true, required = false) })
private List<Inline> inline;

public List<Inline> getInline() {
    return inline;
}

public void setInline(List<Inline> inline) {
    this.inline = inline;
}
}
War es hilfreich?

Lösung

Here the solution, I had to a custom reading function.

@Root(name = "p")
@Convert(value = P.PConvert.class)
public class P extends AtomicBlock{

public P() {
}

@ElementListUnion({

        @ElementList(entry = "gap", type = Gap.class, inline = true, required = false),
        @ElementList(entry = "br", type = Br.class, inline = true, required = false) })
private List<Object> inline;

public List<Object> getInline() {
    return inline;
}

public void setInline(List<Object> inline) {
    this.inline = inline;
}

@Override
public String toString() {
    return "TestP [inline=" + inline + "]";
}

static class PConvert implements Converter<P> {
    private final Serializer ser = new Persister();

    @Override
    public P read(InputNode node) throws Exception {
        P p = new P();

        p.inline = new ArrayList<Object>();
        p.inline.add(node.getValue());
        InputNode inputNode = node.getNext();

        while (inputNode != null) {
            String tag = inputNode.getName();
            if (tag.equalsIgnoreCase("gap")) {
                p.inline.add(ser.read(Gap.class, inputNode));
            } else if (tag.equalsIgnoreCase("br")) {
                p.inline.add(ser.read(Br.class, inputNode));
            }
            String value = node.getValue();
            p.inline.add(value);
            inputNode = node.getNext();
        }

        return p;
    }

    @Override
    public void write(OutputNode arg0, P arg1) throws Exception {
        // TODO Auto-generated method stub

    }
}

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top