I have a client which consumes XML data sent from a server. On connection the server sends a large complete package of data and then periodically sends smaller incremental packages of data.
I use the AsyncStreamsText class to acquire the data:
and then the xml2map class so I can use the JSON parser to extract the required data:
While this method is working well for the small incremental packets of data, the following error is thrown by the initial large package of data:
[Fatal Error] :2:8153: XML document structures must start and end within the same entity.
xml2map._parse2 (java line: 173)
org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 8153; XML document structures must start and end within the same entity.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at anywheresoftware.b4a.objects.SaxParser.parse(SaxParser.java:80)
at anywheresoftware.b4a.objects.SaxParser.Parse(SaxParser.java:73)
at b4j.example.xml2map._parse2(xml2map.java:173)
at b4j.example.xml2map._parse(xml2map.java:161)
at b4j.example.main._fullgamerxd(main.java:622)
at b4j.example.main._astreams_newdata(main.java:338)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.BA$2.run(BA.java:229)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Writing the acquired data to a file and examining it in Notepad++ it appears that after each 8193 characters in the file there is a LF character which is no doubt the cause of the subsequent processing error. As the incremental packages are always less than that, typically 2000 to 3000 characters, there is no problem processing that received data.
So is it likely that the server, over which I have no control, is embedding the unwanted LF characters and if so, what's the easiest method to strip the unwanted (approximately 18) LF characters out of the data?
Of do I need to do something in the AsyncStreamsText or xml2map class to be able to handle the superfluous LF characters?
Thanks,
Bryon
I use the AsyncStreamsText class to acquire the data:
B4X:
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log (msg)
WriteToFile(msg)
FullGameRxd(msg)
End Sub
and then the xml2map class so I can use the JSON parser to extract the required data:
B4X:
Sub FullGameRxd(HTTPText As String)
Dim xm As Xml2Map
xm.Initialize
ParsedData = xm.Parse(HTTPText) ' Parse HTTP text (xml) via Xml2Map
Dim jg As JSONGenerator
jg.Initialize(ParsedData) ' Generate JSON from map
Dim parser As JSONParser
parser.Initialize(jg.ToPrettyString(4)) ' Parse JSON to extract all values
Dim parser As JSONParser
parser.Initialize(jg.ToPrettyString(4))
Dim root As Map = parser.NextObject
While this method is working well for the small incremental packets of data, the following error is thrown by the initial large package of data:
[Fatal Error] :2:8153: XML document structures must start and end within the same entity.
xml2map._parse2 (java line: 173)
org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 8153; XML document structures must start and end within the same entity.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at anywheresoftware.b4a.objects.SaxParser.parse(SaxParser.java:80)
at anywheresoftware.b4a.objects.SaxParser.Parse(SaxParser.java:73)
at b4j.example.xml2map._parse2(xml2map.java:173)
at b4j.example.xml2map._parse(xml2map.java:161)
at b4j.example.main._fullgamerxd(main.java:622)
at b4j.example.main._astreams_newdata(main.java:338)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.BA$2.run(BA.java:229)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Writing the acquired data to a file and examining it in Notepad++ it appears that after each 8193 characters in the file there is a LF character which is no doubt the cause of the subsequent processing error. As the incremental packages are always less than that, typically 2000 to 3000 characters, there is no problem processing that received data.
So is it likely that the server, over which I have no control, is embedding the unwanted LF characters and if so, what's the easiest method to strip the unwanted (approximately 18) LF characters out of the data?
Of do I need to do something in the AsyncStreamsText or xml2map class to be able to handle the superfluous LF characters?
Thanks,
Bryon