Results 1 to 3 of 3

Thread: Parsing XML data into an object

  1. #1

    Default 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

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

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

    Thanks!

  2. #2

    Default

    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. #3

    Default

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

Posting Permissions

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