Giter Site home page Giter Site logo

Comments (12)

caillette avatar caillette commented on September 23, 2024 1

Holy crap, it worked! I upgraded to version 1.1.0 and I fixed the test by creating the AsyncXMLStreamReader with asyncXmlInputFactory. createAsyncForByteArray().

Thanks!

from aalto-xml.

cowtowncoder avatar cowtowncoder commented on September 23, 2024 1

@caillette such an overload already exists:

public abstract AsyncXMLStreamReader<AsyncByteArrayFeeder> createAsyncForByteArray();

and in fact I think it existed before convenience method that creates and feeds content. :)

from aalto-xml.

yar-malik avatar yar-malik commented on September 23, 2024 1

Standalone Runnable Java Code incase somebody wants async xml file reading.


import com.fasterxml.aalto.AsyncByteArrayFeeder;
import com.fasterxml.aalto.AsyncXMLInputFactory;
import com.fasterxml.aalto.AsyncXMLStreamReader;
import com.fasterxml.aalto.stax.InputFactoryImpl;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

public class AkkaGpxTest {

    public ArrayList<LatLon> extractLatLonFromFile() throws XMLStreamException, IOException {

        final AsyncXMLInputFactory f = new InputFactoryImpl();
        AsyncXMLStreamReader<AsyncByteArrayFeeder> sc = null;

        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource("AnyXMLFile.xml").getFile());

        //init array with file length
        byte[] xmlBytes = new byte[(int) file.length()];

        FileInputStream fis = new FileInputStream(file);
        fis.read(xmlBytes); //read file into bytes[]
        fis.close();

        sc = f.createAsyncForByteArray();

        ArrayList<LatLon> latlonList = new ArrayList<LatLon>();

        int bufferFeedLength = 1 ;
        int currentByteOffset = 0 ;
        int type ;
        do{
            while( (type = sc.next()) == AsyncXMLStreamReader.EVENT_INCOMPLETE ) {
                byte[] buffer = new byte[]{ xmlBytes[ currentByteOffset ] } ;
                currentByteOffset ++ ;
                sc.getInputFeeder().feedInput( buffer, 0, bufferFeedLength ) ;
                if( currentByteOffset >= xmlBytes.length ) {
                    sc.getInputFeeder().endOfInput() ;
                }
            }
            switch( type ) {

                case XMLEvent.START_DOCUMENT :
                    System.out.println( "start document" ) ;
                    break ;

                case XMLEvent.START_ELEMENT :
                    System.out.println( "start element: " + sc.getName());
                    break ;

                case XMLEvent.CHARACTERS :
                    System.out.println( "characters: " + sc.getText()) ;
                    break ;

                case XMLEvent.END_ELEMENT :
                    System.out.println( "end element: " + sc.getName()) ;
                    break ;

                case XMLEvent.END_DOCUMENT :
                    System.out.println( "end document" ) ;
                    break ;

                default :
                    break ;
            }
        } while( type != XMLEvent.END_DOCUMENT ) ;
        sc.close();

    }

    public static void main(String[] args) throws IOException, XMLStreamException {

        AkkaGpxTest jt = new AkkaGpxTest();
        jt.extractLatLonFromFile();
    }
}

from aalto-xml.

cowtowncoder avatar cowtowncoder commented on September 23, 2024 1

@AaMalik Thank you for sharing! And yes, I understand no one asks questions to annoy.
Nor should it, it's just that across many repos it sometimes feels questions are asked without reading the full thread. And it is difficult to also know how difficult they are to follow. So frustration I had was more general; apologies for directing it to you and others here.

But the important thing is that there is more information now, and error handling in 1.1.0 is better to also help recognizing the issue. Thank you everyone in helping us get there.

from aalto-xml.

cowtowncoder avatar cowtowncoder commented on September 23, 2024

Bug here is that input is fed twice: first during construction and then again in loop.
Only one of these should be done.

Error message given is not very informative so I hope that can be improved. But the main problem is that feeding document twice will trigger check for XML root element (only one main-level element allowed), which fails -- as it should -- although giving sub-optimal error message.

from aalto-xml.

yar-malik avatar yar-malik commented on September 23, 2024

@caillette did you figure out solution. I am having same problem. What changed did you do?

from aalto-xml.

caillette avatar caillette commented on September 23, 2024

I found no solution. I dropped FasterXML and now use the pull parser from the JRE. I load everything at once in a byte array. I would have liked cowtowncoder to show a solution.

from aalto-xml.

yar-malik avatar yar-malik commented on September 23, 2024

@caillette thanks for this information

@cowtowncoder can you please show a solution to this. I spent 8 hours on this and no luck.

Also the links to documentaiton and code all over internet are outdated so I cant find exaple files.

from aalto-xml.

cowtowncoder avatar cowtowncoder commented on September 23, 2024

@AaMalik @caillette Code in question had a bug -- it was broken. I spelled that out. I don't know what else I can except that "Fix your code -- IT IS BROKEN"?

And fix? REMOVE FEEDING SAME DOCUMENT TWICE. It is first passed when constructing the parser; and then code also passes second copy of the document, byte by byte.
Do one or the other, not both.

What gets me are questions like these: I have answered the question, and yet get asked again. Please, do read the comments before asking for more.

For what it's worth, #52 (included in 1.1.0) would help a bit by giving better error message.

from aalto-xml.

caillette avatar caillette commented on September 23, 2024

@cowtowncoder OK I'm a total moron no problem with that, but I've read my code several times and I could not find out where I feed the document twice. I took the time to write a standalone test case so you can reproduce the bug, or show me how to fix it. You could avoid questions like those by updating code examples.

By the way the EVENT_INCOMPLETE is the only reason for me to use Aalto XML. This feature is so obviously useful that it should be part of the standard by the way. So it will probably asked again if the documentation remains outdated.

from aalto-xml.

cowtowncoder avatar cowtowncoder commented on September 23, 2024

@caillette No, not a moron. Sorry. I do get frustrated, but not due to one question... it's just that total sum of questions gets high. But you just asked just one question, and I can see how it is possible not to find the duplication. So I will try again.

Ok so the problem is this: when constructing parser:

    final AsyncXMLStreamReader< AsyncByteArrayFeeder > xmlStreamReader =
        asyncXmlInputFactory.createAsyncFor( xml.getBytes( Charsets.US_ASCII ) ) ;

you are creating it AND passing document contents in. The whole document.

But in addition, later on, you also do

        inputFeeder.feedInput( buffer, 0, bufferFeedLength ) ;

one byte at a time.

Now, you might reasonably expect to get END_DOCUMENT somewhere, to maybe break out of loop before feeding more content. But unfortunately since parser has no way to know that input has ended -- without calling code informing it, using inputFeeder.endOfInput() ; -- it can not indicate that. This because while the root element has closed, it is legal to have XML comments, processing instructions, which would be valid tokens to report so technically there could be more content to handle.

And as per my other note, I suggest upgrading to 1.1.0 since the error message given should be improved and give a hint as to the problem. Earlier exception message was pretty useless, adding to confusion.

One other thing that may help is that there is one external documentation site:

http://www.studytrails.com/java/xml/aalto/java-xml-aalto-introduction/

which claims to cover asynchronous parsing. I wish there were more real-world examples, but unfortunately I do not work on XML myself these days so I do not have much to share, beyond very simple test cases and basic traversal.
Usage does exist outside, based on questions and comments by authors (for example inside Spring framework I think), but I have not seen much being shared.

from aalto-xml.

yar-malik avatar yar-malik commented on September 23, 2024

@caillette thanks. it works for me now too. This helped a lot.

@cowtowncoder sorry if asking this question annoyed you. It was not the intention. Glad you could keep that on the side and still help. Thanks

from aalto-xml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.