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 20th, 2014, 09:31 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default CreateXMLEventReader does not start with header, why?

Hallo everyone,

I have following xml-file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<MyService version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.blub.com/bla/2.0/final" xmlns="http://www.blub.com/bla/2.0/final" xmlns:lalapop="http://www.blub.com/bla/2.0/final">
...
...
</MyService>
I have essentially following code part:



Code:
...
InputStream input = null;
URL url = null;
...
url = new File(RequestValue.getUri()).toURI().toURL();
input = url.openStream();
...
XMLEventReader xsr = xif.createXMLEventReader(input);
XMLEvent event = xsr.nextEvent();
...
If I debug my code:
Cursor from xsr is START_ELEMENT [MyService xmlns:xsi="http....]
Cursor from event is <?xml version="1.0" encoding='UTF-8' standalone='no'?>

So, it looks like that xsr skipped the header.
Why on earth is it like that? Why doesnt xsr also beginn with the header?

Thank you for your helps.

Regards,

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

If you want to know the actual reason you'll have to find the person who designed it and ask them.

If you want conjecture: most XML parsing applications don't provide access to the XML declaration because the information it contains is designed to be useful for the parser and not for the application.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 22nd, 2014, 07:05 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hallo Mr. Kay,

thank you for your comment.

In the meanwhile, to achieve my goal, I am using XMLStreamReader instead of XMLEventReader. It looks good, but still does not give the desired result. My aim is to split the node "nextTag" from an input file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<firstTag>
	<nextTag>1</nextTag>
	<nextTag>2</nextTag>
</firstTag>
The outcome should look like this:
Code:
<?xml version="1.0" encoding="UTF-8"?><nextTag>1</nextTag>
<?xml version="1.0" encoding="UTF-8"?><nextTag>2</nextTag>
I achieved my goal with this code:
Code:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
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 Demo4 {

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

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

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

		XMLStreamReader streamReader = factory.createXMLStreamReader(in);

		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);

			}

		}

	}

}
Actually very simple. But, my input file can also be in form from a single line:
Code:
<?xml version="1.0" encoding="UTF-8"?><firstTag><nextTag>1</nextTag><nextTag>2</nextTag></firstTag>
My Java code does not produce the desired output anymore, instead just this result:
Code:
<?xml version="1.0" encoding="UTF-8"?><nextTag>1</nextTag>
After spending hours, I am pretty sure to already find out the reason of this:
Code:
t.transform(new StAXSource(streamReader), new StreamResult(writer));
It is because, after the transform method being executed, the cursor will automatically moved forward to the next event. And in the code, I have this fraction:
Code:
while (streamReader.hasNext()) {
	streamReader.next();
                      ...
        t.transform(new StAXSource(streamReader), new StreamResult(writer));
                      ...
}
After the first transform, the streamReader 2 times next():
1. from the transform method
2. from the next method in the while loop

So, in case of this specific line XML, the cursor can never achive the second open tag <nextTag>.
In opposite, if the input XML has a pretty print form, the second <nextTag> can be reached from the cursor because there is a space-event after the first closing tag </nextTag>

Unfortunately, I could not find anything how to do settings, so that the transformator does not automatically spring to next event after performing the transform method. This is so frustating.

Does anybody have any idea how I can deal with it? Also semantically is very welcome. Thank you so much.

Regards,

Ratna

PS. I can surely write a workaround for this problem(pretty print the xml document before transforming it, but this would mean that the input xml was being modified before, this is not allowed)
 
Old June 22nd, 2014, 09:10 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Why not do it in XSLT?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 23rd, 2014, 11:30 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hallo Mr Kay,

yes, XSLT would be my alternative. At this time, I got this code run for my aim. Unfortunately kinda ugly:

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();
		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();}
		}

	}

}
Thank you..

Regards,

Ratna





Similar Threads
Thread Thread Starter Forum Replies Last Post
#header{ tommykehoe BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 0 June 7th, 2012 05:06 PM
header zedjr BOOK: Beginning ASP.NET 4 : in C# and VB 1 August 20th, 2011 08:34 AM
always getting header already sent sun BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 February 2nd, 2011 01:43 PM
How to Add the 'Start Without Debugging' icon next to 'Start With Debugging' icon btcomp BOOK: Beginning ASP.NET 4 : in C# and VB 1 August 1st, 2010 03:41 PM
header Banishah ASP.NET 3.5 Basics 1 August 1st, 2009 03:42 AM





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