开发者

Xml to Object java Spring 3

开发者 https://www.devze.com 2023-04-12 03:31 出处:网络
I have a XML and XSD file corresponds to it. I have just started to learn Spring Framework and I use Spring 3. I should write a code that takes that XML file and assigns it to an object at Java. I sea

I have a XML and XSD file corresponds to it. I have just started to learn Spring Framework and I use Spring 3. I should write a code that takes that XML file and assigns it to an object at Java. I searched about it but how can I do it with 开发者_开发技巧using Spring (maybe some useful tricks or anything else?)


I recently used Spring OXM & JAXB for that. The class is org.springframework.oxm.jaxb.Jaxb2Marshaller. You can, ofcourse, use any other implementation of org.springframework.oxm.Unmarshaller.

But first You'll need to generate the objects based on Your XSD. For that I used maven-jaxb2-plugin.

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <executions>
        <execution>
            <id>generate-oxm</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaDirectory>src/main/resources/META-INF/xsd</schemaDirectory>
                <generatePackage>com.stackoverflow.xjc</generatePackage>
            </configuration>
        </execution>
    </executions>
</plugin>

And then configure the marshaller:

@Configuration
public class ApplicationConfiguration {

    @Autowired
    private ResourcePatternResolver resourceResolver;

    @Bean
    public Jaxb2Marshaller oxmMarshaller() throws IOException {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.stackoverflow.xjc");
        marshaller.setSchemas(resourceResolver.getResources("classpath:/META-INF/xsd/*.xsd"));
        return marshaller;
    }
}

Than just:

File xmlFile = new File("my.xml");
Source source = new StreamSource(new FileInputStream(xmlFile));
JAXBElement<MyXmlRootElemClass> result = oxmMarshaller.unmarshal(source);
MyXmlRootElemClass theObject = result.getValue();


What is exactly the use case ? Imho the best way is what Roadrunner suggests. But if you are using it in some context, like REST requesting and binding xml response, there are nifty abstractions above, like RestTemplate, where you practically don't have to deal with marshaling at all except for creating bean object

restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", Bean.class);

But as I said, if you are just working with XMLs directly, Jaxb2Marshaller is the way.


Spring has nothing to do with it. You need some XML databinding tools. There is a lot of them on the market, and my personal favorite is XStream (http://x-stream.github.io/) with XPP backend. Depending on your objects and xml structure other tools may be more suitable

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号