XStream
  1. XStream
  2. XSTR-374

alias not applied to array elements

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      I have modified the Foo example to nest the array within a class - motivating alias to remove package prefix from xml.
      This seems to work fine - but works with and without alias - the package is interpreted correctly either way.

      When I do the same for a more complex class and package structure, the alias seems to be ignored (read from String) and the package prefix must be in the xml for it to be read back in.
      However, for a single element (everything the same but not array of elements) it seems to work.
      Is it dependent on the level of nesting within the XML? Can't see why it doesn't work. Any ideas on debugging alias use / misuse, esp. with arrays?

      Also - how do you read in an element named 'interface'? You can't map it to a java field! Is there a usable XSLT example and would that be the way to resolve?

      What nature of validation is done on the inbound XML? support for .xsd?
      Thanks!

      xstream.alias("TaskLookupByReviewerReponse", TaskLookupByReviewerResponse.class);
      doesn't seem to map correctly for array elements. It works fine for a single entry.

      "<message><serviceHeader><interfaceName>x</interfaceName><name>getY</name><consumerId>z</consumerId><consumerVersion>1</consumerVersion><logLevel>E</logLevel><instrumentInd>N</instrumentInd><returnCode>0</returnCode><reasonCode>0</reasonCode><returnText>NA</returnText></serviceHeader>" +
      "<messageBody>" +
      "<TaskLookupByReviewerResponses>"+
      "<taskList.TaskLookupByReviewerResponse>"+
      and then this is the repeated element content

      The following works, but seems to work with and without alias specified the same on write and read.
      I will try to add more xml depth until it breaks, but maybe it is simpler thant that!


      import com.thoughtworks.xstream.XStream;

      import FooPkg.Foo;
      import FooPkg.FooWrapper;

      public class FooTest
      {

      public static String toXML() throws Exception

      { XStream xstream = new XStream(); xstream.alias("Foo", Foo.class); xstream.alias("FooWrapper", FooWrapper.class); FooWrapper fw = new FooWrapper(100); String xmlMessage = xstream.toXML(fw); String decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; String totalXml = decl + xmlMessage; return totalXml; }

      public static void fromXML(String theXml) throws Exception
      {
      XStream xstream = new XStream();
      xstream.alias("Foo", Foo.class);
      xstream.alias("FooWrapper", FooWrapper.class);

      try
      {
      FooWrapper fw = (FooWrapper)xstream.fromXML(theXml);

      String a = fw.a;
      Foo[] f = fw.foos;
      System.out.println("fw.a = " + a);
      for (int i=0; i<f.length; i++)

      { System.out.println("a = " + f[i].a + ";i = " + f[i].i); }

      }
      catch(Exception e)

      { System.out.println(e.getMessage()); e.printStackTrace(); }

      }

      public static void main(String[] args) throws Exception

      { String theXml = toXML(); System.out.println("created Xml:" + theXml); fromXML(theXml); }

      }


      package FooPkg;

      public class Foo
      {
      public String a = "a";
      public int i = 1;
      }


      package FooPkg;

      import FooPkg.Foo;
      public class FooWrapper {
      public String a;
      public Foo[] foos;

      public FooWrapper(int n)
      {
      this.foos = new Foo[n];
      for(int i = 0; i < n; i++)

      { foos[i] = new Foo(); }

      this.a = Integer.toString;
      }
      }

        People

        • Assignee:
          Unassigned
          Reporter:
          mike f
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: