Comments (12)
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.
@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.
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.
@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.
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.
@caillette did you figure out solution. I am having same problem. What changed did you do?
from aalto-xml.
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.
@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.
@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.
@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.
@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.
@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)
- Source bundles have no Bundle-SymbolicName in Manifest HOT 4
- XML Escape is not working HOT 1
- Can't use ENTITY_REFERENCE event for resolution in an Attribute HOT 3
- Support disabling General Entity Reference replacement in attribute values HOT 6
- XMLReaderImpl.getAttributeValue(String, String) does not ignore namespace if null HOT 1
- Add Java 9+ module info using Moditect HOT 1
- Reading full elements
- `BufferRecycler` access not safe if readers/writers used from different threads
- Support disabling namespace awareness HOT 1
- java.lang.NoClassDefFoundError: javax/xml/stream/events/XMLEvent HOT 2
- getLocation() doesn't count xml prolog for non-utf8 documents
- Add support for JEP-185 (JAXP-1.5) properties named ACCESS_EXTERNAL_
- Async parsing turns " inside element content into apostrophe HOT 4
- DTD RootName only returned correctly if it consists of a multiple of 4 bytes HOT 1
- `Attributes.getValue(String qName)` sometimes returns values from previous elements HOT 4
- `DataUtil.growArrayBy(arr, more)` is vulnerable to break the service. HOT 1
- ` textAccumulator.addText(buf, start, end)` introduces security issue. HOT 1
- Multi-byte characters are split in writeCData() if first byte sits right at the end of the buffer HOT 3
- Multi-byte characters are split in `writeComment()` if first byte sits right at the end of the buffer
- Multi-byte characters are split in `writePI()` if first byte sits right at the end of the buffer
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aalto-xml.