Re: [Simple-support] had parsing working for strings, but now need to do so for ArrayList
Brought to you by:
niallg
|
From: meghiddo <mat...@ho...> - 2009-06-06 06:54:51
|
I resolved this on my own
In my measurementProject class I had declarations for devices, permissions,
and channels (the three that were giving me trouble) but I had them declared
as Strings instead of an ArrayList. I changed that, and then I had one
other problem, but all I had to do was change my properties field from
required=true to required=false, and it worked immediately.
The way I figred it out was, I just played around with that readXml method,
by erasing methods that came after a period,
Then I would hit the period so it would pop up with the list of all possible
methods, along with descriptions of what they did, and I read through those.
In doing so I got a better understanding of how simplexml works, and then
the idea just came to me (after reading through a few more lines in the
stack).
meghiddo wrote:
>
> I had code working that took data from a web service, parsed it, and
> loaded the values into a list that I could use in a rich:tree. This was
> working just fine when it took the data from this xml:
>
> [code] <?xml version="1.0" encoding="UTF-8" ?>
> - <deviceCollection>
> - <device>
> <deviceId>exampleDevice1</deviceId>
> <name>Example Device 1</name>
> </device>
> - <device>
> <deviceId>WSN-Module-8F3A-D247-4B99</deviceId>
> <name>WSN CRio Device</name>
> </device>
> </deviceCollection> [/code]
>
> But now I need to convert it to take the data from a different xml set up
> that looks like this:
>
> [code] - <measurementProject>
> <projectId>1</projectId>
> <name>matt</name>
> <permissions class="java.util.ArrayList" />
> <devices class="java.util.ArrayList" />
> <channels class="java.util.ArrayList" />
> </measurementProject> [/code]
>
> But when I did so I get this exception:
>
> [code]org.simpleframework.xml.core.InstantiationException: Type class
> java.util.ArrayList is not compatible with class java.lang.String
> at org.simpleframework.xml.core.Factory.getOverride(Factory.java:105)
> at
> org.simpleframework.xml.core.PrimitiveFactory.getInstance(PrimitiveFactory.java:66)
> at org.simpleframework.xml.core.Primitive.readElement(Primitive.java:181)
> at org.simpleframework.xml.core.Primitive.read(Primitive.java:119)
> at org.simpleframework.xml.core.Composite.readObject(Composite.java:520)
> at org.simpleframework.xml.core.Composite.read(Composite.java:477)
> at org.simpleframework.xml.core.Composite.readElement(Composite.java:458)
> at
> org.simpleframework.xml.core.Composite.readElements(Composite.java:374)
> at org.simpleframework.xml.core.Composite.read(Composite.java:259)
> at org.simpleframework.xml.core.Composite.read(Composite.java:204)
> at org.simpleframework.xml.core.Composite.read(Composite.java:158)
> at org.simpleframework.xml.core.Composite.read(Composite.java:135)
> at org.simpleframework.xml.core.Traverser.read(Traverser.java:78)
> at
> org.simpleframework.xml.core.CompositeInlineList.read(CompositeInlineList.java:182)
> at
> org.simpleframework.xml.core.CompositeInlineList.read(CompositeInlineList.java:159)
> at
> org.simpleframework.xml.core.CompositeInlineList.read(CompositeInlineList.java:117)
> at org.simpleframework.xml.core.Composite.readObject(Composite.java:520)
> at org.simpleframework.xml.core.Composite.read(Composite.java:477)
> at org.simpleframework.xml.core.Composite.readElement(Composite.java:458)
> at
> org.simpleframework.xml.core.Composite.readElements(Composite.java:374)
> at org.simpleframework.xml.core.Composite.read(Composite.java:259)
> at org.simpleframework.xml.core.Composite.read(Composite.java:204)
> at org.simpleframework.xml.core.Composite.read(Composite.java:158)
> at org.simpleframework.xml.core.Composite.read(Composite.java:135)
> at org.simpleframework.xml.core.Traverser.read(Traverser.java:78)
> at org.simpleframework.xml.core.Persister.read(Persister.java:544)
> at org.simpleframework.xml.core.Persister.read(Persister.java:526)
> at org.simpleframework.xml.core.Persister.read(Persister.java:507)
> at org.simpleframework.xml.core.Persister.read(Persister.java:489)
> at org.simpleframework.xml.core.Persister.read(Persister.java:471)
> at org.simpleframework.xml.core.Persister.read(Persister.java:452)
> at
> projectContainer.MeasurementProjectCollectionXMLAO.readXML(MeasurementProjectCollectionXMLAO.java:17)
> at
> testContainer.ProjectTreeTest.loadProjectNodes(ProjectTreeTest.java:34)
> at testContainer.TempMain.main(TempMain.java:11) [/code]
>
> So it is obvious that ther is a problem with parsing this new xml due to
> the fact it has these three lines:
>
> [code] <permissions class="java.util.ArrayList" />
> <devices class="java.util.ArrayList" />
> <channels class="java.util.ArrayList" />
> [/code]
>
> Things were fine when it was only parsing Strings, but there is a problem
> with the parsing of the ArrayLists.
> I'm not sure where to look to find the problem and how to solve it
> honestly.
> If I were to guess I would say there will need to be a change made to:
>
> [code]public class MeasurementProjectCollectionXMLAO {
>
> public MeasurementProjectCollection readXML(InputStream xml) {
> Serializer serializer = new Persister();
>
> MeasurementProjectCollection measurementProjectCollection = null;
> try {
> measurementProjectCollection =
> serializer.read(MeasurementProjectCollection.class, xml);
> } catch (Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> try {
> xml.close();
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> return measurementProjectCollection;
> } [/code]
>
>
> But I really do not know where. If it is any help, here is the class that
> calls the xml parser, and then loads the values to a list.
>
>
> [code]public void loadProjectNodes() {
>
> MeasurementProjectCollectionXMLAO access = new
> MeasurementProjectCollectionXMLAO();
> ProjectHttpClient method = new ProjectHttpClient();
> MeasurementProjectCollection measurementProjectCollection = null;
>
>
> try {
> measurementProjectCollection = access.readXML(method.Response(null));
> } catch (Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
>
> //System.out.println("printing deviceCollection =
> access.readXML(method.getResponseBodyAsStream(null));: "
> //+ deviceCollection.toString());
>
> for (MeasurementProject measurementProject :
> measurementProjectCollection.getMeasurementProjects()){
> String x = measurementProject.getName();
> projectNameList.add(x);
> //System.out.println(ids);
> }
>
> }
>
> public List<String> getProjectNameList() {
> //loadNodes();
> return projectNameList;
> }
>
> public void setProjectnameList(List<String> projectNameList) {
> this.projectNameList = projectNameList;
> }
>
> public void print(){
> System.out.println("Printing projectNameList values: "+projectNameList);
> }
> } [/code]
>
> As soon as I try to call loadProjectNodes I get that exception though.
>
> I have to get this working by Tuesday. May seem like plenty of time but
> with my track record on these kind of things...
>
--
View this message in context: http://www.nabble.com/had-parsing-working-for-strings%2C-but-now-need-to-do-so-for-ArrayList-tp23896552p23899261.html
Sent from the Simple XML Serialization mailing list archive at Nabble.com.
|