Announcement Announcement Module
No announcement yet.
Parsing XML data into an object Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parsing XML data into an object

    Hi, I'm new to Spring and web services, but please bear with me. I would like to build an application that retrieves xml http requests and loads/parses the data from the xml into an object. Is there something already built into Spring that handles this? Ideally, I would like to be able to have the URL of the xml feed included in my applicationContext.xml and connected to my handler mapping somewhere in the block of XML below, so that listBooks loads the data from the corresponding URL

    <bean id="portletModeParameterHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeParameterHandlerMapping">
            <property name="order" value="1"/>
    		<property name="interceptors">
    				<ref bean="parameterMappingInterceptor"/>
    		<property name="portletModeParameterMap">
    				<entry key="view">
    						<entry key="listBooks" value-ref="bookController"/>
    						<entry key="viewBook" value-ref="bookViewController"/>
    Here is an example of the xml:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    		<book id="222">
    			<title>The Jungle</title>
    		<book id="235">
    			<title>To Kill a Mockingbird</title>
    Any help would be greatly appreciated.


  • #2
    To retrieve xml data into objects you must first define your object class and then parse that data into an instance.
    1. 'create object' operation may be completed by coding class manually or (more natural) you could write an xml schema that your feed will conformed with and generate classes from schema.
    2. 'parse that data' is library-dependant operation, but generally if in step 1. you've created schema and produced java sources from that schema with some tool, that tool will have api to deserialize (unmarshal) input streams, strings, etc containing xml.

    So take a look at JAXB, XMLBeans.

    All that you need to create by hand is an xml schema, this is the best solution to such problems imo.


    • #3
      Hey, exgorth. XMLBeans appears to be exactly what I was looking for, thank you!