How to verify and interpret XML files in WebLogic

Source: Internet
Author: User

How to verify and interpret XML files in WebLogic

The project used XML verification and explanation the other day, so I wrote a validator. There was no problem in the local test, but at WebLogic, there was a problem with the program. Later, I found some information to solve the problem. The problem is caused by JAXP.

The verification class is as follows (only the main code may need to be debugged ):

Import org. Apache. log4j. Logger;

Import org. JDOM. Document;

Import org. JDOM. Input. saxbuilder;

Import org. JDOM. Output. xmloutputter;

 

Import javax. xml. parsers. documentbuilder;

Import javax. xml. parsers. documentbuilderfactory;

Import java. Io .*;

 

Public class defaultparser {

Public static final string schema_language = "http://java.sun.com/xml/jaxp/properties/schemaLanguage ";

Public static final string xml_schema = "http://www.w3.org/2001/XMLSchema ";

Public static final string schema_source = "http://java.sun.com/xml/jaxp/properties/schemaSource ";

Private Static logger log = logger. getlogger (defaultparser. Class );

 

Public Boolean validate (inputstream in, inputstream schema, errorhandle)

Throws exception {

Log. debug ("defaultparser. Validate () begin :");

Boolean result = false;

Documentbuilderfactory factory = documentbuilderfactory. newinstance ();

Factory. setnamespaceaware (true );

Factory. setvalidating (true );

Factory. setattribute (schema_language, xml_schema );

Factory. setattribute (schema_source, schema );

Documentbuilder parser = factory. newdocumentbuilder ();

Parser. seterrorhandler (errorhandle );

 

Parser. parse (in );

 

If (errorhandle. getmsglist (). Size () = 0 ){

Result = true;

}

Log. debug ("defaultparser. Validate () Result:" + result );

Return result;

}

}

The above Code does not have any problems in IDE debugging, and XML errors can be correctly verified. However, when the program is released to WebLogic, an invalid parameter error occurs, this is mainly because documentbuilderfactory uses the built-in WebLogic implementation class. The implementation class in xerces is used for local testing (Org. Apache. xerces. JAXP. documentbuilderfactoryimpl ). Look at the original documentbuilderfactory. newinstance () code. We can see that the default implementation class is Sun's implementation class (Org. Apache. Crimson. JAXP. documentbuilderfactoryimpl ). Therefore, the implementation classes are different in different environments. Since we have not figured out how to configure or specify the implementation classes, we have to directly use the classes that can run in the test environment. Modify the following lines in the above Code:

Documentbuilderfactory factory = new org. Apache. xerces. JAXP. documentbuilderfactoryimpl ();

 

The modified verification method is as follows:

Public Boolean validate (inputstream in, inputstream schema, errorhandle)

Throws exception {

Log. debug ("defaultparser. Validate () begin :");

Boolean result = false;

Documentbuilderfactory factory = new org. Apache. xerces. JAXP. documentbuilderfactoryimpl ();

Factory. setnamespaceaware (true );

Factory. setvalidating (true );

Factory. setattribute (schema_language, xml_schema );

Factory. setattribute (schema_source, schema );

Documentbuilder parser = factory. newdocumentbuilder ();

Parser. seterrorhandler (errorhandle );

 

Parser. parse (in );

 

If (errorhandle. getmsglist (). Size () = 0 ){

Result = true;

}

Log. debug ("defaultparser. Validate () Result:" + result );

Return result;

}

 

I know there are better solutions, and I hope you can correct them. Thank you.

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.