You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by le...@email.arizona.edu on 2008/01/31 01:49:15 UTC

[Betwixt] Unable to get XML into Collection - always empty

Hi,

I have an existing XML file format that I'm trying to mapping into beans with
Betwixt (so I can't tweak the elements and attributes).  I'm able to get one of
the collections read just fine - but the other is failing to load and just
returning empty.  In both cases, I'm relying on Betwixt to automatically
determine the collection relation with plural/singular forms.  If I attempt to
state the element mapping in the .betwixt file, it causes the whole thing to
not load.

I slimmed down my example from the my concrete case (hopefully I slimmed it down
enough).

So the XML looks like this:
===========================
<?xml version="1.0" standalone="yes" ?>

<root>
   <node id="99999">
   	<name>Heliconius</name>
    <othernames>
      <othername>
        <name>Heliconius cydno pachinus</name>
      </othername>
    </othernames>
    <nodes>
    	<node id="666666">
    		<name>taxa 1</name>
    	</node>
    	<node id="777777">
    		<name>taxa 2</name>
    	</node>
    </nodes>
   </node>
</root>

My object model is pretty simple, XTRoot -> root, XTNode -> node, and
XTOtherName -> othername.  I attached the java source. Each class has a default,
no argument constructor.  And XTNode has a getter/setter for the collections
along with a singular add*() method, so getNodes() and addNode(XTNode) /
getOtherNames() and addOtherName(XTOtherName)

I'm just stated .betwixt files (not sure if doing a single betwixt-config would
be better)

XTNode.betwixt
==========================
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes="element">
  <element name="node">
  	<attribute name="id" property="id" />
	<attribute name="has-page" property="hasPage" />
<!-- add this in the file causes the whole load of the node element to fail
  	<element name="othernames" property="othernames" />
  -->
    <addDefaults />
  </element>
</info>

XTOtherName.betwixt
==========================
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes="element">
  <element name="othername">
    <addDefaults/>
  </element>
</info>

XTRoot.betwixt
==========================
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes="element">
  <element name="root">
    <element name="node" property="node" />
    <addDefaults />
  </element>
</info>

At the moment - I'm just trying to test the success of load with a simple
class-main-method setup (nothing fancy):

try {

	InputStream xtnodes =
EntryPointX.class.getResourceAsStream("./Heliconius_othernames_simple.xml");
	System.out.println("$> xml loaded...");
	BeanReader beanReader = new BeanReader();
	beanReader.registerBeanClass("othername", XTOtherName.class);
	beanReader.registerBeanClass(XTRoot.class);
	beanReader.registerBeanClass(XTNode.class);

	XTRoot root = (XTRoot)beanReader.parse(xtnodes);
	System.out.println("$> xml parsed...");
	if (root != null) {
		XTNode node = root.getNode();
		if (node != null) {
			System.out.println("node-id: " + node.getId());
			System.out.println("nodes: " + node.getNodes());

			System.out.println("othernames(size:"+node.getOtherNames().size()+"): " +
node.getOtherNames());
		}
	}
	System.out.println("$> done...");
} catch (Exception e) {
	System.out.println("exception occurred... lame");
	e.printStackTrace();
}

When I run this - I get a collection loaded for Nodes with two elements as I'd
expect.  But OtherNames is just empty (because I'm setting it to an empty
collection in the XTNode class).

The JDK version I'm using is jdk1.5.0_14

I'm not a maven user (sorry).  But here's a listing of the jars on my classpath:

commons-beanutils-1.7.0.jar
commons-beanutils-bean-collections.jar
commons-beanutils-core.jar
commons-betwixt-0.8.jar
commons-collections-3.1.jar
commons-digester-1.8.jar
commons-io-1.1.jar
commons-lang-2.1.jar
commons-logging-1.0.4.jar
commons-pool-1.1.jar
xercesImpl-2.6.2.jar
xml-apis-2.0.2.jar

I'm rather stumped why one of the two works and the other does not.  I looked
through and saw that people had similar issues but I wasn't able to follow the
solutions (or they looked like they were having slightly different issues).  I
went through the FAQ and don't think I missed a simple solution (other than the
hypothesis that XTOtherName.class isn't being loaded or resolved properly).

This is the first question I've asked on the list - so if I'm done or said
something egregious I'll apologize in advance.

Thanks for reading - I appreciate it.

Andy



Re: [Betwixt] Unable to get XML into Collection - always empty

Posted by le...@email.arizona.edu.
I think I figured out my issues.

It appears to be my collection type-naming.  I had xml elements to 
represent the
collection as:

<othernames>
   <othername>
   </othername>
</othernames

My Java Beans for the element was XTOtherName.  It seems that the PascalCasing
of the classname threw off the plural/singular resolution for the
adder-functions.  When I changed it to XTOthername and the corresponding
methods (addOthername() & getOthernames() - instead of addOtherName() &
getOtherNames()) to not have the uppercase 'N' the adder-functions were
resolved and the collection was no longer empty.

No one responding about the issue - I just thought I'd follow-up with what I
think fixed my issue so if anyones search the mailing-archive they won't flail
around looking for a solution to this like I did.

Andy




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org