Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2014, 11:34 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Transformer, how to disable validation?

Hallo everyone,

I have this code part:
Code:
InputStream inputStream = new FileInputStream("input.xml");
XMLInputFactory factory = XMLInputFactory.newInstance();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
XMLStreamReader streamReader = factory.createXMLStreamReader(inputStream);
...
t.transform(new StAXSource(streamReader), new StreamResult(writer));
...
How can I disable the internal validation from the transform() method? Is this possible?

Thank you..

Regards,
Ratna
 
Old June 23rd, 2014, 11:44 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Check the properties on the XMLInputFactory. The property javax.xml.stream.isValidating should be false. The XSLT transformer itself won't perform DTD validation, but the XML parser may.

When people ask for this, they sometimes mean not that they want no validation, but that they don't want the DTD to be read. It's not possible to suppress reading the DTD, because it is needed for purposes (e.g. entity expansion) other than validation.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 23rd, 2014, 04:31 PM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hallo Mr Kay,

thank you for the quick help.

I tried adding:
Code:
factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
Unfortunately, it does not changes, I still get ParseError-Exception.
Code:
Exception in thread "main" javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,6]
Message: Verarbeitungsanweisungsziel, das "[xX][mM][lL]" entspricht, ist nicht zulässig.
	at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
	at transformation.activity.Demo6.weiter(Demo6.java:59)
	at transformation.activity.Demo6.main(Demo6.java:40)
pa
My input file looks exactly like the following, and I actually just want to extract each nextTag with all contents nextTag propably has.:
Code:
<?xml version="1.0" encoding="UTF-8"?><firstTag><nextTag><adutan>1</adutan></nextTag></firstTag>
<?xml version="1.0" encoding="UTF-8"?><firstTag><nextTag>2</nextTag></firstTag>
Each line is a well conformed XML document.
By using StaX, if the parser reached <nextTag>, I let this tag be transformed from transformer. Unfortunately, the transformer seems to be irritated from the xml declaration on the 2nd line.

I actually just expect to get:
Code:
<?xml version="1.0" encoding="UTF-8"?><nextTag><adutan>1</adutan></nextTag>
<?xml version="1.0" encoding="UTF-8"?><nextTag>2</nextTag>
The header is in the transformation output is retrieved automatically, because OutputKeys.OMIT_XML_DECLARATION is per default NO.

I actually can try to get each line separately and transform it, also separately, but maybe is it possible to get multiple conform XML documents in a file parsed at one time?

Thank you for your comments.

Regards,

Ratna

My complete code looks like this:
Code:
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;

public class Demo6 {

	public static void main(String[] args) throws Exception {

		InputStream inputStream = new FileInputStream("input.xml");

		XMLInputFactory factory = XMLInputFactory.newInstance();
		factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer t = tf.newTransformer();

		XMLStreamReader streamReader = factory
				.createXMLStreamReader(inputStream);

		while (streamReader.hasNext()) {
			streamReader.next();

			if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT
					&& "nextTag".equals(streamReader.getLocalName())) {
		

				StringWriter writer = new StringWriter();
				t.transform(new StAXSource(streamReader), new StreamResult(
						writer));
				String output = writer.toString();
				System.out.println(output);

				weiter(streamReader, t);

			}

		}

	}

	private static void weiter(XMLStreamReader streamReader,
			Transformer transformer) throws Exception {
		while (streamReader.hasNext()) {
			if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT
					&& "nextTag".equals(streamReader.getLocalName())) {
				StringWriter writer1 = new StringWriter();
				transformer.transform(new StAXSource(streamReader),
						new StreamResult(writer1));
				String output1 = writer1.toString();
				System.out.println(output1);

			} else {streamReader.next();}
		}

	}

}
 
Old June 23rd, 2014, 05:22 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

OK, that's nothing to do with validation: your file is not a well-formed XML document, because it contains more than one XML declaration.

Your best bet might be to split the file into multiple XML documents using some non-XML process, e.g. a sed script.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 24th, 2014, 08:20 PM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hallo Michael,

thank you for your suggestion.

I finally found a way to successfully parse this kind of file the way I want:
Code:
<?xml version="1.0" encoding="UTF-8"?><firstTag><nextTag>1</nextTag></firstTag>
<?xml version="1.0" encoding="UTF-8"?><firstTag><nextTag>2</nextTag></firstTag>
Code:
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;

import javolution.xml.internal.stream.XMLInputFactoryImpl;
import javolution.xml.stream.XMLInputFactory;
import javolution.xml.stream.XMLStreamConstants;
import javolution.xml.stream.XMLStreamReader;

import javax.xml.stream.XMLEventReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;

public class Demo7 {

	public static void main(String[] args) throws Exception {

		InputStream inputStream = new FileInputStream("input.xml");
		InputStreamReader reader = new InputStreamReader(inputStream);

		XMLInputFactory factory = new XMLInputFactoryImpl();
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer t = tf.newTransformer();

		XMLStreamReader streamReader = factory.createXMLStreamReader(reader);

		while (streamReader.hasNext()) {
			streamReader.next();
			
			if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT && streamReader.getLocalName().equals("nextTag")) {

				StringWriter writer = new StringWriter();
				// this does not work
				// t.transform(new StAXSource(streamReader),
				//		new StreamResult(writer));
				String output = writer.toString();
				System.out.println(output);

				weiter(streamReader, t);

			}

		}

	}

	private static void weiter(XMLStreamReader streamReader,
			Transformer transformer) throws Exception {
		while (streamReader.hasNext()) {
			if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT
					&& "nextTag".equals(streamReader.getLocalName())) {
				StringWriter writer1 = new StringWriter();
				// transformer.transform(new StAXSource(streamReader),
				// new StreamResult(writer1));
				String output1 = writer1.toString();
				System.out.println(output1);

			} else {
				streamReader.next();
			}
		}

	}

}
this is so crazy:

The magic jar is: javolution.xml.*

The XMLStreamReader.next() method from this StaX implementation does one thing the normal javax.xml.stream.XMLStreamReader.next() unable to !

Based on the above contents , javolution can still go to next line and recognize all the events of this line. javax.xml.stream.XMLStreamReader.next() can not (because of the XML declaration in the 2nd line?), it throws exception.

The way I took to extract a particular tag with all the corresponding contents was by using Transformer.transform:
t.transform(new StAXSource(javax.xml.stream.XMLStreamReader), new StreamResult(java.io.StringWriter));

This is so frustating:

Using javolution, unfortunately I can not create StAXSource from the XMLStreamReader in order to be used in Transformer.transform:
t.transform(new StAXSource(xMLStreamReader), new StreamResult(stringWriter));

Any idea anyone, using javax.xml.transform.Transformer, how I can transform a tag/event from XMLStreamReader of javolution?

Thank you..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation controls/enable-disable 07Raghav BOOK: Beginning ASP.NET 4 : in C# and VB 2 December 29th, 2012 03:16 PM
Disable Request Validation stormage BOOK: Beginning ASP.NET 4 : in C# and VB 1 September 12th, 2010 03:29 AM
XSLT 2.0 Transformer for Java albinjoseph XSLT 1 March 2nd, 2007 04:05 AM
transformer does not detect some tags in my srcXML mishoch XSLT 0 August 9th, 2006 09:08 AM
Exception in generating a new transformer neta XSLT 5 September 23rd, 2004 04:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.