B4J Question Help needed opening XML file

rfresh

Well-Known Member
Licensed User
Longtime User
I'm trying to open an XML file (f as String) and then read it in a parser that wants it as an InputStream.

How do I convert the file read from a String to an InputStream?

See line 44.

Thank you...

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private btnFileDialog As Button
    Private fc As FileChooser
    Private parser As SaxParser
    Private f As String
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    MainForm.Title = "Convert .fpl To *.fms"
End Sub

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)

End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If parser.Parents.IndexOf("item") > -1 Then
        If Name = "waypoint" Then
            Log(Text.ToString)
        End If
    End If
End Sub

Sub btnFileDialog_Click
    fc.Initialize
    fc.InitialDirectory = "C:\Documents\3DGarmin430"
    fc.setExtensionFilter("ForeFlightImage", Array As String("*.fpl"))
    f = fc.ShowOpen(MainForm)
    Log(f)

    Dim in As InputStream
    in = File.OpenInput(File.DirAssets, f) 'This file was added with the file manager.
    parser.Parse(f, "Parser") '"Parser" is the events subs prefix.
    in.Close
   
End Sub
 

Attachments

  • ForeFlight.zip
    801 bytes · Views: 152
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Could you not just change 'f' to 'in' in this line as you already created the inputstream on the previous line

B4X:
parser.Parse( in , "Parser") '"Parser" is the events subs prefix.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Yes, that removed the error (I just didn't see that).

Line 9 gives me a file not found error, but the log() msg at line 6 shows the full path filename IS correct!!

B4X:
Sub btnFileDialog_Click
    fc.Initialize
    fc.InitialDirectory = "C:\Users\rfresh1011\Documents\3DGarmin430"
    fc.setExtensionFilter("ForeFlight", Array As String("*.fpl"))
    f = fc.ShowOpen(MainForm)
    Log(f)
    'f2 = StringToInputStream(f)
    Dim in As InputStream
    in = File.OpenInput(File.DirAssets, f) 'This file was added with the file manager.
    parser.Parse(in, "Parser") '"Parser" is the events subs prefix.
    in.Close
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Try removing file.dirassets from this line ( the file name from the file selector should contain the full path)
B4X:
in = File.OpenInput( "", f) 'This file was added with the file manager.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I get a java.lang.NullPointerException

I'll step back and take another look at the example I found on this board. The only thing different I am doing is using FileChooser to get the filename. But obviously I'm missing something else in the example code, so I'll have another good look at it.

Thank you...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Never use SaxParser. Always use Xml2Map.
2. The file encoding is wrong. It says utf 8 but there is a BOM marking of a different encoding. I've converted it to UTF8 with Notepad++.

B4X:
Sub AppStart (Args() As String)
    Dim x2m As Xml2Map
    x2m.Initialize
    Dim m As Map = x2m.Parse(File.ReadString("C:\Users\H\Downloads\ForeFlight.fpl", ""))
    Log(m)
    'easier to understand the structure with:
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Log(jg.ToPrettyString(4))
End Sub
 
Upvote 0
Top