Easy implementation of JavaBeans to XML conversion

Source: Internet
Author: User
Tags object model web services xml parser xsl

-->The portability and scalability of Java and XML make them ideal for addressing this issue in order to flexibly meet the changing needs of Web applications and Web services. SAX (simple APIs for XML), DOM (Document Object Model), XSL (extensible Stylesheet Language), XSLT (XSL Transformations), S OAP (Simple Object Access Protocol), and BML (Bean Markup Language) is the relevant technology in the XML domain. This article brings together the lightweight and scalable Java and XML benefits, but does not require developers to understand the relevant technologies described above.

Use remote method Invocation (RMI) In a Java distributed application instead of directly using the underlying socket or other network link code. EJB technology also frees developers from the underlying mechanisms of transaction,recovery, activation, and so on. Similarly, using the Javabean-xml mapping component of this article, developers do not have to deal directly with XML-related APIs.


-->

  Compilation of components

The Beanxmlmapping component implements the conversion from JavaBean to XML files. With the JavaBean introspection mechanism, XML Parser and Dom APIs, you can write a ToXml () method to implement transformations from JavaBean to XML files, and the FromXml () method to implement transformations from XML files to JavaBean.

Listing 1 shows a possible implementation of the beanxmlmapping component. The Jox (Java Objects in XML) class library is used in the program. Of course, you can also use other APIs to implement the Beanxmlmapping component, where Jox is chosen because it is simpler and reusable.

List 1 beanxmlmapping components

Import com.wutka.jox.*;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;

public class Beanxmlmapping {

/**
* Retrieves a Bean object for the
* Received XML and matching Bean class
*/
public static Object FromXml (String XML, Class className) {
Bytearrayinputstream xmlData = new Bytearrayinputstream (Xml.getbytes ());
Joxbeaninputstream joxin = new Joxbeaninputstream (xmlData);
try {
Return (Object

<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.