Project

Profile

Help

Command Line to Java Program

Added by Sathya Selva about 3 years ago

java net.sf.saxon.Query -qs:. -s:source.xml -dtd:on


Replies (20)

Please register to reply

RE: Command Line to Java Program - Added by Michael Kay about 3 years ago

Sorry, was this intended to be a question?

RE: Command Line to Java Program - Added by Sathya Selva about 3 years ago

I saw only xslt related help on all over internet, not xquery, i saw only command line process only but i want a java code for that, if you post here then it will usefull to everyone.

Thanks in advance

RE: Command Line to Java Program - Added by Michael Kay about 3 years ago

Information on how to invoke XQuery from a Java application can be found here:

https://saxonica.com/documentation/index.html#!using-xquery/api-query

RE: Command Line to Java Program - Added by Sathya Selva about 3 years ago

Please give some example

RE: Command Line to Java Program - Added by Martin Honnen about 3 years ago

For simple programs see QueryX methods in https://saxonica.plan.io/projects/saxon/repository/he/revisions/master/entry/latest10/samples/java/he/S9APIExamples.java, the file should also be in the resources download.

If you want to see the code of the rather more complex net.sf.saxon.Query then it is at https://saxonica.plan.io/projects/saxon/repository/he/revisions/master/entry/latest10/hej/net/sf/saxon/Query.java, it should also be in the src zip download.

RE: Command Line to Java Program - Added by Sathya Selva about 3 years ago

Hi !

While I'm using command line : java -cp "saxon9ee.jar" net.sf.saxon.Query -qs:. -s:"source.xml" -dtd:on

I'm Getting Result is :

Recoverable error on line 11 column 8 of source.xml:
  SXXP0003: Error reported by XML parser: Element type "sathya" must be declared.
Recoverable error on line 51 column 15 of source.xml:
  SXXP0003: Error reported by XML parser: The content of element type "ArticleInfo" must
  match "(ArticleID,ArticleExternalID?,ArticleDOI?,ArticleCitationID?,ArticleSequenceNumber?,ArticleRelatedObject*,(ArticleSuperTitle?,ArticleTitle,ArticleSubTitle?)+,ArticleClassification?,ArticleCategory?,ArticleSubCategory?,ArticleCollection*,ArticleFirstPage?,ArticleLastPage?,ArticleHistory?,ChangeHistory?,ArticleEditorialResponsibility?,ArticleFundingInformation?,ArticleCopyright,ArticleGrants?,ArticleContext?)".
Recoverable error on line 951 column 25 of source.xml:
  SXXP0003: Error reported by XML parser: Attribute "test" must be declared for element type
  "FamilyName".
Query processing failed: The XML parser reported three validation errors.

by using java code :

XdmNode source = documentBuilder
				.build(new StreamSource(new StringReader(string)));
		documentBuilder.setLineNumbering(true);
		XQueryCompiler xQueryCompiler = processor.newXQueryCompiler();
		processor.setConfigurationProperty(Feature.RECOVERY_POLICY, Configuration.RECOVER_SILENTLY);
		processor.setConfigurationProperty(Feature.DTD_VALIDATION, true);
		XQueryExecutable xQueryExecutable = xQueryCompiler.compile(".");
		XQueryEvaluator xQueryEvaluator = xQueryExecutable.load();
		Serializer out = processor.newSerializer();
		out.setOutputProperty(Serializer.Property.METHOD, "text");
		StringWriter stringwriter = new StringWriter();
		out.setOutputWriter(stringwriter);
		xQueryEvaluator.setContextItem(source);
		xQueryEvaluator.setDestination(out);
		xQueryEvaluator.run();
		System.out.println(stringwriter.toString());

i'm getting same xml as a text format , i want command line output here also please correct if i'm wrong anywhere

Thanks in advance

RE: Command Line to Java Program - Added by Martin Honnen about 3 years ago

Which value does string have in new StreamSource(new StringReader(string))? If you have a file (e.g. source.xml) you want to load you don't use a StringReader, you open a new StreamSource(new File("source.xml")). Would not to check, however, whether that suffices to set up DTD validation.

RE: Command Line to Java Program - Added by Martin Honnen about 3 years ago

https://www.saxonica.com/html/documentation9.9/dotnetdoc/Saxon/Api/DocumentBuilder.html#DtdValidation suggests you need to make sure you set documentBuilder.setDTDValidation(true) explicitly.

RE: Command Line to Java Program - Added by Sathya Selva about 3 years ago

i'm tried with file also but it show the same xml as a text format

RE: Command Line to Java Program - Added by Sathya Selva about 3 years ago

XdmNode source = documentBuilder
				.build(new StreamSource(new File("source.xml")));
		documentBuilder.setLineNumbering(true);
		documentBuilder.setDTDValidation(true);

After using above code, i got the same result xml as the text format

RE: Command Line to Java Program - Added by Martin Honnen about 3 years ago

Well, first set the parsing options

            documentBuilder.setLineNumbering(true);
            documentBuilder.setDTDValidation(true);

then parse by calling the build method and make sure you catch any exeception the build method might throw.

RE: Command Line to Java Program - Added by Sathya Selva about 3 years ago

please give a sample, i tried with your advice but there is no improment, maybe it might my misunderstanding.

RE: Command Line to Java Program - Added by Martin Honnen about 3 years ago

       Processor processor = new Processor(true);

        DocumentBuilder docBuilder = processor.newDocumentBuilder();

        docBuilder.setDTDValidation(true);

        XdmNode input;
        try
        {
           input = docBuilder.build(new File("source.xml")); 
        }
        catch (SaxonApiException e)
        {
            System.out.println(e);
        }

should suffice to report parsing errors during document parsing/building.

RE: Command Line to Java Program - Added by Sathya Selva about 3 years ago

Is there any error listener is available for storing the error into String

We don't currently have a direct way in the DocumentBuilder class of capturing errors. However, the build() method accepts a Source, and you could supply a StreamSource initialised with an XmlReader on which you have set an error handler using XmlReader.setErrorHandler().

You could also supply an AugmentedSource which is a Saxon-supplied implementation of the JAXP Source interface allowing additional properties to be supplied, including an ErrorReporter.

RE: Command Line to Java Program - Added by Martin Honnen about 3 years ago

I think, that one way, is to access the Configuration from the Processor and use https://www.saxonica.com/html/documentation9.9/javadoc/net/sf/saxon/Configuration.html#setErrorListener-javax.xml.transform.ErrorListener-. You can try that and see whether it serves your needs, otherwise explain in more detail what you are looking for.

RE: Command Line to Java Program - Added by Sathya Selva about 3 years ago

Processor processor = new Processor(true);

        DocumentBuilder docBuilder = processor.newDocumentBuilder();

        docBuilder.setDTDValidation(true);

        XdmNode input;
        try
        {
           input = docBuilder.build(new File("source.xml")); 
        }
        catch (SaxonApiException e)
        {
            System.out.println(e);
        }

When i using this code, i got below result, but the result was only print on the console, i want the result as a string, i want to modify the error data and after print the error.

Recoverable error on line 11 column 8 of source.xml:
  SXXP0003: Error reported by XML parser: Element type "sathya" must be declared.
Recoverable error on line 51 column 15 of source.xml:
  SXXP0003: Error reported by XML parser: The content of element type "ArticleInfo" must
  match "(ArticleID,ArticleExternalID?,ArticleDOI?,ArticleCitationID?,ArticleSequenceNumber?,ArticleRelatedObject*,(ArticleSuperTitle?,ArticleTitle,ArticleSubTitle?)+,ArticleClassification?,ArticleCategory?,ArticleSubCategory?,ArticleCollection*,ArticleFirstPage?,ArticleLastPage?,ArticleHistory?,ChangeHistory?,ArticleEditorialResponsibility?,ArticleFundingInformation?,ArticleCopyright,ArticleGrants?,ArticleContext?)".
Recoverable error on line 951 column 25 of source.xml:
  SXXP0003: Error reported by XML parser: Attribute "test" must be declared for element type
  "FamilyName".
Query processing failed: The XML parser reported three

RE: Command Line to Java Program - Added by Sathya Selva about 3 years ago

Anyone Please Provide the Solution.

Thanks in advanced.

RE: Command Line to Java Program - Added by Martin Honnen about 3 years ago

I am not sure there is something like "the Solution".

I think by default Saxon sets up a StandardLogger on the Configuration which logs messages to System.err. You can change that to set up your own Logger or to have the StandardLogger not write to the error console but your own PrintStream e.g. perhaps over a ByteArrayOutputStream to store the messages in memory:

        Processor processor = new Processor(true);

        Configuration configuration = processor.getUnderlyingConfiguration();

        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        PrintStream logStream = new PrintStream(bs, true, "utf-8");
        configuration.setLogger(new StandardLogger(logStream));

        DocumentBuilder docBuilder = processor.newDocumentBuilder();

        docBuilder.setDTDValidation(true);

        XdmNode input;
        try
        {
           input = docBuilder.build(new File("source.xml")); 
        }
        catch (SaxonApiException e)
        {
            System.out.println("Input parsing failed: " + e.getMessage());
            System.out.println("Logged messages: " + bs.toString());
        }

        logStream.close();

So in that sample bs.toString() gives you the messages captured by the logger as a string.

See the API docs with https://www.saxonica.com/html/documentation9.9/javadoc/net/sf/saxon/lib/StandardLogger.html#setPrintStream-java.io.PrintStream-, https://www.saxonica.com/html/documentation9.9/javadoc/net/sf/saxon/Configuration.html#setLogger-net.sf.saxon.lib.Logger-, https://www.saxonica.com/html/documentation9.9/javadoc/net/sf/saxon/s9api/Processor.html#getUnderlyingConfiguration--

RE: Command Line to Java Program - Added by Sathya Selva about 3 years ago

Above the code working fine but some XML file result line number is mismatched.

Error found in attribute shown as line 8372, it is the last line of the document. But I created the error in 995th line.

RE: Command Line to Java Program - Added by Michael Kay about 3 years ago

Please start a new thread with the new question, and be precise: attach the files so that we can reproduce the problem.

There are some errors that can only be detected at the end, for example an IDREF attribute for which there is no corresponding ID. But you haven't told us anything about what this error actually is.

    (1-20/20)

    Please register to reply