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.