commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Gough" <David.Go...@leeds.orange.co.uk>
Subject Betwixt - Strings set to null strings when reading a Collection of strings
Date Fri, 25 Nov 2005 09:13:05 GMT
I have been trying to get Betwixt to read/write my java classes and have
run into a problem with read Collections of Strings.

 

Included is a very simple test to write out a collection of strings to a
file and read them back. The output is fine, but when I read the Strings
back, they are set to "null". Am I doing something wrong. I have scoured
the docs and can't see a problem.

 

Here's the class I am I am testing with

 

import java.util.ArrayList;

import java.util.List;

 

 

public class TestStrings {

    

    List testStrings = new ArrayList();

    

    

    /** Creates a new instance of TestStrings */

    public TestStrings() {

    }

    

    public List getTestStrings() {

        return testStrings;

    }

    

    public void addTestString(String testString) {

        System.out.println("Adding testString: " + testString);

        testStrings.add(testString);

    }

    

}

 

And the teststrings.betwixt file:

 

<?xml version="1.0" encoding="UTF-8" ?>

<info>

    <element name="test">

 

        <element name="teststrings">

            <element name="teststring" property="testStrings"
updater="addTestString"/>

        </element>

 

    </element>

</info>

 

 

 

Some example code to read/write

 

public void testStringsWriteRead() throws Exception {

        System.out.println("testStringsWriteRead");

        String outFile = "teststrings.xml";

        FileOutputStream fos = new FileOutputStream(outFile);

        TestStrings testStrings = new TestStrings();

        testStrings.addTestString("test1");

        testStrings.addTestString("test2");

        BeanWriter writer = new BeanWriter(fos);

        writer.writeXmlDeclaration("<?xml version=\"1.0\" ?>");

        writer.setWriteIDs(false);

        writer.enablePrettyPrint();        

        writer.write(testStrings);     

        

       FileInputStream fis = new FileInputStream(outFile);

        BeanReader reader = new BeanReader();

        setReaderProperties(reader);

        reader.registerBeanClass(new
InputSource(TestStrings.class.getResourceAsStream("teststrings.betwixt")
), TestStrings.class);

        TestStrings testStrings2 = (TestStrings)reader.parse(new
InputSource(outFile));

        

        // Write out again to console

        writer = new BeanWriter();

        writer.getXMLIntrospector().setAttributesForPrimitives(false);

        writer.setWriteIDs(false);

        writer.enablePrettyPrint();        

        writer.write(testStrings2);                   

    }      

    

 

output - teststring.xml:

 

<?xml version="1.0" ?>

  <test>

    <teststrings>

      <teststring>test1</teststring>

      <teststring>test2</teststring>

    </teststrings>

  </test>

 

testStrings2 does not have any strrings in it's list when it is read
from the example output above.

 

What am I doing wrong?

 

Regards,

 

Dave Gough



Important.
Confidentiality: This communication is intended for the above-named person and may be confidential
and/or legally privileged. Any opinions expressed in this communication are not necessarily
those of the company. If it has come to you in error you must take no action based on it,
nor must you copy or show it to anyone; please delete/destroy and inform the sender immediately.

Monitoring/Viruses

Orange may monitor all incoming and outgoing emails in line with current legislation. Although
we have taken steps to ensure that this email and attachments are free from any virus, we
advise that in keeping with good computing practice the recipient should ensure they are actually
virus free.

Orange PCS Limited is a subsidiary of Orange SA and is registered in England No 2178917, with
its address at St James Court, Great Park Road, Almondsbury Park, Bradley Stoke, Bristol BS32
4QJ.
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message