Results 1 to 13 of 13
Thread: parsing Object to XML
- 11-19-2009, 06:29 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
parsing Object to XML
Hi,
I have a variable of type Object, which is obtained from an xml-rpc call like:
and I know that it contains this xml data:Java Code:Object[] params = new Object[]{new Double(4.0), new Double(5.0)}; Object result = client.execute( "foobar.add", params );
I also tested it this way:Java Code:<methodResponse> <params> <param> <value><struct> <member><name>value1</name> <value><double>4.2</double></value> </member> <member><name>value2</name> <value><double>9.6</double></value> </member> <member><name>product</name> <value><double>13.8</double></value> </member> </struct></value> </param> </params> </methodResponse>
and I got the result as:Java Code:System.out.println(result.toString());
but what I need is, to insert the obtained data to a database, and I don't know how to extract these obtained values for my table filed in the database!Java Code:{product=9.0, value1=4.0, value2=5.0}
Could anybody help me to find out, what is the easiest method to extract data and insert to the database?
Thank you! :)
- 11-19-2009, 07:52 PM #2
you could send that result into a constructor that separates the values(take double between = and ,) and then write each object with it's three values into your database.
this is assuming all the info you're pulling from the xml is formatted that same way, also assuming I understood your question(no sleep last night. Thanks alot modern warfare 2)Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 11-19-2009, 08:11 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
xcallmejudasx, thanks for the answer!
This is possible, but the an inefficient solution considering a general case, when I want to shift from this simple test to database manipulation! Imagine a big table, a big response...and seeking a lot of "= ,". There must be some effective way...
I just tried like:
and generated error which I couldn't figure it out, why it can't read the output string of the "result"!Java Code:DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(result.toString())); Document doc = db.parse(is); NodeList nodes = doc.getElementsByTagName("member");
Who can help? :(
- 11-19-2009, 08:22 PM #4
What was the error that can help determine what caused it.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 11-19-2009, 08:32 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
[Fatal Error] :1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException: Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMPars er.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBu ilderImpl.parse(Unknown Source)
at ipkgXmlPrcTechTest.xmlrpcHandler.main(xmlrpcHandle r.java:66)
Obviously the source can not be defined as String, but it is really String, and I didn't find a source of Object type!
- 11-19-2009, 08:38 PM #6
Have you tried casting your String to Object and sending it in that way? Technically it shouldn't matter but if your parser is being picky perhaps it will believe the disguise.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 11-19-2009, 09:03 PM #7
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
my "result" is an Object. Passing it as it is, did not work, also passing it as String did not work...but I can't believe that there is not a sophisticated way to extract data from a hidden struct inside an Object!
- 11-20-2009, 09:56 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
The reason the parser is choking is, according to your first post, you are supplying result.toString() to it...which is:
{product=9.0, value1=4.0, value2=5.0}
and that's not XML.
ETA: You result must be more than simply an Object class...Object does not contain any of the data you say is in result. Cast result to whatever its actual class is.
- 11-22-2009, 07:42 PM #9
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
I can not use other that Object to obtain the result of the xml rpc response, as it does not allowed. But I found a lib called XStream, that serialized my Object called result, to xml!!
So the only thing remained is to know how efficiently I can record xml to db. The xml is now in this format:
Do you know any tutorial that gives a simple example, how to insert an xml answer to a database?Java Code:<element> <string>something</string> <double>some number</double> </element>
- 11-23-2009, 08:52 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Not allowed? What silly requirement is that?
Your object is of some class...not just an Object.
Or are you saying that the thing your calling could be sending any old class over, in which case that's a slightly poor design.
As for sticking it in a database, I'd be interested to know how you think you ought to go about doing that? I have no idea what the data you're looking at is, so trying to tell you how to structure your table would be a tad difficult.
- 11-23-2009, 03:12 PM #11
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
Ok this is right the incomming data. Please tell me if you knew that you receive such data, how did you implemented the receiver?
<methodResponse>
<params> <param>
<value><struct>
<member><name>value1</name> <value><double>4.2</double></value> </member>
<member><name>value2</name> <value><double>9.6</double></value> </member>
<member><name>product</name> <value><double>13.8</double></value> </member>
</struct></value>
</param> </params>
</methodResponse>
I implemented it as an Object, because there are both String as name of fields and also Doubles, so I wrote:
Object result = client.execute( "foobar.add", params );
- 11-23-2009, 03:22 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
The API for java.lang.Object.
Where in that does it allow you to set any attributes?
It doesn't...
Therefore what you have coming back from your call to the client is a child class (as all classes are) of Object.
If you used a debugger to step over that call you'd be able to see exactly what class it is that's turned up. It's not a String, since toString() gave you something that looks a bit like JSON, and you say the actual data is in XML (though you don't say how you know that).
So, what is the actual class of this object?
- 11-25-2009, 11:34 AM #13
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
Tolls, thank you for the post.
You want to say that a hidden type casting is taking place. Ok I will check it with the debugger and bring the result back here.
>though you don't say how you know that
Well I wrote the php server part that sends the data by myself. It is an xml-rpc service of Joomla! that is sending the result for sure in xml. I also tested this, by using a tool called xml rpc debugger you can google to see, and got the xml data. The thing is that without using the XStream class, I can't get the data in xml form, and yet XStream changes some of my nodes by its own node types. May be debugging as you say helps to know how to obtain data right in xml but not in a hidden-converted object.
Similar Threads
-
Operator < cannot be applied to java.lang.Object, Object
By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 02:12 AM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Parsing a superclass object to subclass object dynamicly
By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:27 PM -
Creating Document object for XML parsing
By Java Tip in forum Java TipReplies: 0Last Post: 11-19-2007, 04:12 PM -
Creating object of Type Object class
By venkatv in forum New To JavaReplies: 3Last Post: 07-17-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks