Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find if a node exists in XStream api

Tags:

java

xstream

I have an XML structure similar to following. I have converters to write each object A, B, and C. Is it possible in XStream to check if the a node exists in the XML hierarchy? i.e in the case below, I want to do something if B node exists before I write C.

<A>
    <B>
        <C>
        </C>
    </B>
</A>    
like image 461
somename Avatar asked Feb 05 '19 00:02

somename


1 Answers

If I understand correctly, you want to check in CConverter whether B node already exists in hierarchy. If the structure is as above that's always is true. Marshalling process starts from the root object and goes into internal properties. So, to write C node first B must exist.

Assume you have simple POJO structure like below:

class A {
    public B b = new B();
}

class B {
    public C c = new C();
}

class C {

}

Now, we can implement converter for A and B:

class AConverter implements Converter {

    public boolean canConvert(Class clazz) {
        return clazz.equals(A.class);
    }

    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
        System.out.println("Marshalling A!");
        A a = (A) value;
        if (a.b != null) {
            writer.startNode("B");
            context.convertAnother(a.b);
            writer.endNode();
        }
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        throw new NotImplementedException();
    }
}

class BConverter implements Converter {

    public boolean canConvert(Class clazz) {
        return clazz.equals(B.class);
    }

    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
        System.out.println("Marshalling B!");
        B b = (B) value;
        if (b.c != null) {
            writer.startNode("C");
            context.convertAnother(b.c);
            writer.endNode();
        }
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        throw new NotImplementedException();
    }
}

Main class:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class XStreamApp {
    public static void main(String[] args) {
        XStream xStream = new XStream();
        xStream.registerConverter(new AConverter());
        xStream.registerConverter(new BConverter());
        xStream.alias("A", A.class);

        System.out.println(xStream.toXML(new A()));
    }
}

Prints:

Marshalling A!
Marshalling B!
<A>
  <B>
    <C/>
  </B>
</A>
like image 122
Michał Ziober Avatar answered Oct 07 '22 09:10

Michał Ziober