Results 1 to 13 of 13
  1. #1
    kievari is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Default parsing Object to XML

    Hi,
    I have a variable of type Object, which is obtained from an xml-rpc call like:

    Java Code:
    Object[] params = new Object[]{new Double(4.0), new Double(5.0)};
    Object result = client.execute( "foobar.add", params );
    and I know that it contains this xml data:
    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>
    I also tested it this way:
    Java Code:
    System.out.println(result.toString());
    and I got the result as:
    Java Code:
    {product=9.0, value1=4.0, value2=5.0}
    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!

    Could anybody help me to find out, what is the easiest method to extract data and insert to the database?

    Thank you! :)

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  3. #3
    kievari is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Default

    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:
    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");
    and generated error which I couldn't figure it out, why it can't read the output string of the "result"!

    Who can help? :(

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  5. #5
    kievari is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Default

    [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!

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  7. #7
    kievari is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Default

    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!

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    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.

  9. #9
    kievari is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Default

    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:
    Java Code:
    <element>
      <string>something</string>
      <double>some number</double>
    </element>
    Do you know any tutorial that gives a simple example, how to insert an xml answer to a database?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    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. #11
    kievari is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Not allowed? What silly requirement is that?
    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 );

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    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?

  13. #13
    kievari is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Default

    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

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  3. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  4. Creating Document object for XML parsing
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-19-2007, 04:12 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •