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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PatlaDJ

    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(St ring 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(qNa me)) {
    //Do
    something...
    }

    }
  • Martin Honnen

    #2
    Re: Java SAX parser. How to get the raw XML code of the currentlyparsin g event

    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(St ring 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

    Comment

    • PatlaDJ

      #3
      Re: Java SAX parser. How to get the raw XML code of the currentlyparsin g event

      On 3 Þëè, 15:30, Martin Honnen <mahotr...@yaho o.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(St ring 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 -InputStreamRead er -BufferedReader -InputSource -then
      it goes to .parse(InputSou rce)....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 ?

      Comment

      • Joseph J. Kesselman

        #4
        Re: Java SAX parser. How to get the raw XML code of the currentlyparsin g event

        Socket -InputStreamRead er -BufferedReader -InputSource -then
        it goes to .parse(InputSou rce)....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 InputStreamRead er and the InputSource -- whether
        upstream, downstream, or in place of the BufferedReader depends on the
        details of how this tee adapter is written.

        Comment

        • PatlaDJ

          #5
          Re: Java SAX parser. How to get the raw XML code of the currentlyparsin g event

          On 3 àÌÉ, 17:34, "Joseph J. Kesselman" <keshlam-nos...@comcast. net>
          wrote:
          Socket -InputStreamRead er -BufferedReader -InputSource -then
          it goes to .parse(InputSou rce)....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 InputStreamRead er 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 RecordingInputS tream that wraps the
          InputStream
          here is the class source code:

          import java.io.ByteArr ayOutputStream;
          import java.io.FilterI nputStream;
          import java.io.InputSt ream;
          import java.io.IOExcep tion;

          /**
          *
          * @author Unknown
          *
          */

          class RecordingInputS tream extends FilterInputStre am {
          protected ByteArrayOutput Stream sink;

          RecordingInputS tream(InputStre am in) {
          this(in, new ByteArrayOutput Stream());
          }

          RecordingInputS tream(InputStre am in, ByteArrayOutput Stream 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.toByteArra y();
          }

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

          Comment

          • jimmy Zhang

            #6
            Re: Java SAX parser. How to get the raw XML code of the currently parsing event

            SAX is a bit outdated... try either Pull or VTD-XML (http://vtd-xml.sf.net)

            "PatlaDJ" <patladj@gmail. comwrote in message
            news:2aaf86a4-5a78-4334-a607-8cdc1a4d9082@a7 0g2000hsh.googl egroups.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(St ring 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(qNa me)) {
            //Do
            something...
            }
            >
            }

            Comment

            Working...