0

I have an class like below and I use xSteam to convert Edge to xml.

@XStreamAlias("edge")
class Edge{

     @XStreamAsAttribute
     @XStreamAlias("source")
     private String source;

     @XStreamAsAttribute
     @XStreamAlias("target")
     private String target;

     @XStreamAlias("data")
     private Data data;
     .....
}

When I set data=null, i can get

  <edge source="8" target="10" />

but I want to get below when data =null

  <edge source="8" target="10" ></edge>

Some one can help for this?

Steven
  • 3
  • 2
  • 1
    Why does it matter? The two forms are identical (in fact with most XML parsing technologies you have no way of knowing which form was used in the XML you are parsing, they both show up as an element with two attributes and no text content or children). – Ian Roberts Jul 18 '12 at 15:14

2 Answers2

2

you can use Dom4JDriver driver. in that case you can format your xml output like this code:

OutputFormat outPutFormat = new OutputFormat();
outPutFormat.setLineSeparator("");
outPutFormat.setExpandEmptyElements(true);
outPutFormat.setEncoding("UTF-8");

Dom4JDriver d4j = new Dom4JDriver(new XmlFriendlyNameCoder("_", "_"));
d4j.setOutputFormat(outPutFormat);
XStream xstream = new XStream(d4j);
xstream.autodetectAnnotations(true);

0

There is nothing you can do on the XML level, as on that level, both forms are completely identical. If you really really require one form instead of the other, you have to either provide your own XML serializer, or use the default serializer and post-process its output stream. i cannot give you more details without knowing how you currently serialize your data.

MvG
  • 57,380
  • 22
  • 148
  • 276