A xmlbean tip

Source: Internet
Author: User
Xmlbean is indeed a very convenient tool. It can generate Java classes based on XSD, so that we can read and write XML in a very comfortable object-oriented way. the xmldocument and xmlnode objects in the net world are more intuitive. However, the documents in the Java World are not flattering. Code The crude Java Doc produced is treated as API ref and cannot be compared with Microsoft's msdn. This is when I use multiple Java development tools and Lib, this impression has been deepened again and again.
Some time ago, I used xmlbean to generate xml strings. My process was like this: 1. I used xmlspy to write the XSD and then used xmlbean to generate a Java class for me. 2. Use these Java classes to create a document. The data source is database 3. Obtain the XML string of the generated document. 3. Adapt to Web service to send these XML strings to a remote server. 4. The remote server verifies the received XML based on XSD to check whether it complies with the specifications. As a result, the remote server ( Program Is written by another company, but the same XSD is used.) The verification fails. After checking, I found that the node labels of the XML Element generated by xmlbean are not exactly the same as those defined in XSD. For example, I defined in XSD:

< Xs: Element Name = "Myverylongtagname" >

The generated XML is

< Mvltn > </ Mvltn >

On the surface, xmlbean generates the abbreviated form of tag for me, which is not what I want. I checked the crude Java Doc of xmlbean and tried multiple APIs to solve the problem. Now I will list the small tip: to ensure that the Tag Name of the generated XML element is exactly the same as that defined in XSD, You Need To Do additional work, such: Myxmldocument Doc = Myxmldocument. Factory. newinstance ();
// Create and set XML node Value

// Set xmloptions
Xmloptions OPT =   New Xmloptions ();
Map =   New Hashmap ();
Map. Put ( " My XML namespace " , " Xmlnsnameinxml " );
Map. Put ( " My element Tag Name " , " Elementtagnameinxml " );
// Set use implicit namespaces
Opt. setsaveimplicitnamespaces (MAP );

// Get XML string
String xmlstring = Doc. xmltext (OPT );

After this setting, xmlbean will not intelligently set the long element Tag Name and xml ns defined in XSD as simple abbreviations.

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.