Projet d'exemple Java
Le code Java listé ci-dessous montre comment accéder aux fonctions de base. Il est structuré dans les différentes parties suivantes :
•Situer le dossier d'exemple, et créer une instance d'objet COM RaptorXML
•Effectuer une transformation XSLT, retourner le résultat en un string
•Traiter un document XQuery, retourner le résultat en un string
Cette fonction de base est incluse dans les fichiers dans le dossier examples/API du dossier d'application RaptorXML Server.
public class RunRaptorXML
{
// Locate samples installed with the product
// (will be two levels higher from examples/API/Java)
// REMARK: You might need to modify this path
static final String strExamplesFolder = System.getProperty("user.dir") + "/../../" ;
static com.altova.raptorxml.RaptorXMLFactory rxml;
static void ValidateXML() throws com.altova.raptorxml.RaptorXMLException
{
com.altova.raptorxml.XMLValidator xmlValidator = rxml.getXMLValidator();
System.out.println("RaptorXML Java - XML validation");
xmlValidator.setInputFromText( "<!DOCTYPE root [ <!ELEMENT root (#PCDATA)> ]> <root>simple input document</root>" );
if( xmlValidator.isWellFormed() )
System.out.println( "The input string is well-formed" );
else
System.out.println( "Input string is not well-formed: " + xmlValidator.getLastErrorMessage() );
if( xmlValidator.isValid() )
System.out.println( "The input string is valid" );
else
System.out.println( "Input string is not valid: " + xmlValidator.getLastErrorMessage() );
}
static void RunXSLT() throws com.altova.raptorxml.RaptorXMLException
{
System.out.println("RaptorXML Java - XSL Transformation");
com.altova.raptorxml.XSLT xsltEngine = rxml.getXSLT();
xsltEngine.setInputXMLFileName( strExamplesFolder + "simple.xml" );
xsltEngine.setXSLFileName( strExamplesFolder + "transform.xsl" );
String result = xsltEngine.executeAndGetResultAsString();
if( result == null )
System.out.println( "Transformation failed: " + xsltEngine.getLastErrorMessage() );
else
System.out.println( "Result is " + result );
}
static void RunXQuery() throws com.altova.raptorxml.RaptorXMLException
{
System.out.println("RaptorXML Java - XQuery execution");
com.altova.raptorxml.XQuery xqEngine = rxml.getXQuery();
xqEngine.setInputXMLFileName( strExamplesFolder + "simple.xml" );
xqEngine.setXQueryFileName( strExamplesFolder + "CopyInput.xq" );
System result = xqEngine.executeAndGetResultAsString();
if( result == null )
System.out.println( "Execution failed: " + xqEngine.getLastErrorMessage() );
else
System.out.println( "Result is " + result );
}
public static void main(String[] args)
{
try
{
rxml = com.altova.raptorxml.RaptorXML.getFactory();
rxml.setErrorLimit( 3 );
ValidateXML();
RunXSLT();
RunXQuery();
}
catch( com.altova.raptorxml.RaptorXMLException e )
{
e.printStackTrace();
}
}
}