Пример #1
0
  @Test
  public void testPersonListFormEmptyElement() {
    //		XStream xstream = new XStream(new DomDriver());
    XStream xstream = new XStream();
    xstream.processAnnotations(TestPersonAnnotationList.class);
    xstream.processAnnotations(TestPersonAnnotation.class);
    xstream.processAnnotations(TestPerson.class);
    //		xstream.registerConverter(new EmptyTestPersonAnnotationConverter());

    TestPersonAnnotation hh = new TestPersonAnnotation();
    hh.setUserName("hanhan");
    hh.setAge(30);
    TestPerson parent = new TestPerson();
    //		parent.setAge(11);
    hh.setParent(null);

    TestPersonAnnotationList list = new TestPersonAnnotationList();
    list.list.add(hh);
    String xmlStr = xstream.toXML(list);
    /*StringWriter sw = new StringWriter();
    PrettyPrintWriter writer = new CompactWriter(sw);
    xstream.marshal(list, writer);
    String xmlStr = sw.toString();*/

    System.out.println("testPersonListFormEmptyElement xml:\n " + xmlStr);

    xmlStr =
        XmlUtils.toXML(
            Lists.newArrayList(hh), "list", ArrayList.class, "person", TestPersonAnnotation.class);
    System.out.println("testPersonListFormEmptyElement xml22:\n " + xmlStr);
  }
Пример #2
0
 @Override
 public void marshal(
     Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
   TestPersonAnnotation p = (TestPersonAnnotation) source;
   writer.startNode("name");
   writer.setValue(p.getUserName());
   writer.endNode();
   writer.startNode("age");
   writer.setValue("" + p.getAge());
   writer.endNode();
   writer.startNode("parent");
   writer.setValue("");
   writer.endNode();
 }