473,414 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Java SAX parser. How to get the raw XML code of the currently parsingevent

Java SAX parser, please need a clue how to get the raw XML code of the
currently parsing event... needed for logging, debugging purposes.

Here's and example, letting me clarify exactly what i need: (see the
comments in source)

public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//..Here... or maybe somewhere elsewhere I need on my disposal the raw
XML code of every XML tags received from the XML stream.
//.. I need simply to write them down in a log file, for debugging
purposes, while parsing.
//.. Can anyone give me a suggestion how can i do that logging while
the SAX parser only returns me the tagname and attributes. While
parsing I want to log the XML code for every tag in its 'pure form',
like it is comming from the server directly on the socket's input
reader.

if ("p".equals(qName)) {
//Do
something...
}

}
Jul 2 '08 #1
5 7695
PatlaDJ wrote:
Java SAX parser, please need a clue how to get the raw XML code of the
currently parsing event... needed for logging, debugging purposes.

Here's and example, letting me clarify exactly what i need: (see the
comments in source)

public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//..Here... or maybe somewhere elsewhere I need on my disposal the raw
XML code of every XML tags received from the XML stream.
I don't think the SAX API provides access to the raw XML.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 3 '08 #2
On 3 Þëè, 15:30, Martin Honnen <mahotr...@yahoo.dewrote:
PatlaDJ wrote:
Java SAX parser, please need a clue how to get the raw XML code of the
currently parsing event... needed for logging, debugging purposes.
Here's and example, letting me clarify exactly what i need: (see the
comments in source)
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//..Here... or maybe somewhere elsewhere I need on my disposal the raw
XML code of every XML tags received from the XML stream.

I don't think the SAX API provides access to the raw XML.

--

* * * * Martin Honnen
* * * *http://JavaScript.FAQTs.com/
Yes... now i'm pretty sure that it doesn't provide such access. Too
bad l;(

Right now i'm working towards clonning my input stream. Currently i'm
trying to clone the bufferedReader i pass as an inputsourse to the
parser ....i can't get it done :(

Let me explain: I read from a Socket ... the chain sequence is as
follows:

Socket -InputStreamReader -BufferedReader -InputSource -then
it goes to .parse(InputSource)....of the SAX.

What node on this chain can be cloned so I can read the data two
times.....once for the parser, once for my debugging log.

Am I on the right track, or maybe not ?
Jul 3 '08 #3
Socket -InputStreamReader -BufferedReader -InputSource -then
it goes to .parse(InputSource)....of the SAX.

What node on this chain can be cloned so I can read the data two
times.....once for the parser, once for my debugging log.
I'd suggest dealing with it at the Java level. Find or write a "wrapper"
reader implementation which saves a copy of the data passing through it
off to a data structure or scratch file for rereading, or perhaps which
writes the data direct to your log as it passes through. Plug that in
somewhere between the InputStreamReader and the InputSource -- whether
upstream, downstream, or in place of the BufferedReader depends on the
details of how this tee adapter is written.
Jul 3 '08 #4
On 3 àÌÉ, 17:34, "Joseph J. Kesselman" <keshlam-nos...@comcast.net>
wrote:
Socket -InputStreamReader -BufferedReader -InputSource -then
it goes to .parse(InputSource)....of the SAX.
What node on this chain can be cloned so I can read the data two
times.....once for the parser, once for my debugging log.

I'd suggest dealing with it at the Java level. Find or write a "wrapper"
reader implementation which saves a copy of the data passing through it
off to a data structure or scratch file for rereading, or perhaps which
writes the data direct to your log as it passes through. Plug that in
somewhere between the InputStreamReader and the InputSource -- whether
upstream, downstream, or in place of the BufferedReader depends on the
details of how this tee adapter is written.
YES!

I've solved my problem using class RecordingInputStream that wraps the
InputStream
here is the class source code:

import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;

/**
*
* @author Unknown
*
*/

class RecordingInputStream extends FilterInputStream {
protected ByteArrayOutputStream sink;

RecordingInputStream(InputStream in) {
this(in, new ByteArrayOutputStream());
}

RecordingInputStream(InputStream in, ByteArrayOutputStream sink)
{
super(in);
this.sink = sink;
}

public synchronized int read() throws IOException {
int i = in.read();
sink.write(i);
return i;
}

public synchronized int read(byte[] buf, int off, int len) throws
IOException {
int l = in.read(buf, off, len);
sink.write(buf, off, l);
return l;
}

public synchronized int read(byte[] buf) throws IOException {
return read(buf, 0, buf.length);
}

public synchronized long skip(long len) throws IOException {
long l = 0;
int i = 0;
byte[] buf = new byte[1024];
while (l < len) {
i = read(buf, 0, (int)Math.min((long)buf.length, len -
l));
if (i == -1) break;
l += i;
}
return l;
}

byte[] getBytes() {
return sink.toByteArray();
}

void resetSink() {
sink.reset();
}
}

Jul 3 '08 #5
SAX is a bit outdated... try either Pull or VTD-XML (http://vtd-xml.sf.net)

"PatlaDJ" <pa*****@gmail.comwrote in message
news:2a**********************************@a70g2000 hsh.googlegroups.com...
Java SAX parser, please need a clue how to get the raw XML code of the
currently parsing event... needed for logging, debugging purposes.

Here's and example, letting me clarify exactly what i need: (see the
comments in source)

public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//..Here... or maybe somewhere elsewhere I need on my disposal the raw
XML code of every XML tags received from the XML stream.
//.. I need simply to write them down in a log file, for debugging
purposes, while parsing.
//.. Can anyone give me a suggestion how can i do that logging while
the SAX parser only returns me the tagname and attributes. While
parsing I want to log the XML code for every tag in its 'pure form',
like it is comming from the server directly on the socket's input
reader.

if ("p".equals(qName)) {
//Do
something...
}

}

Aug 4 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
13
by: Ajay | last post by:
hi! can you call a Python application from a Java program? does this require any additional package to be installed? thanks cheers
0
by: Juan Manuel Fernández Luna | last post by:
Can anybody help me? I'm desperate!!! I'm writing a program in Java to deal with xml files and when the file volume.xml (attached) is being parsed, the following exception occurs and I don't...
1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: Nageshwararao | last post by:
i getting path of XML file and parsing it. Following is error i am getting. can any one help me. Exception in thread "main" java.lang.InternalError...
318
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.