Android Question parsing file with sax parser

acorrias

Member
Licensed User
Longtime User
HI
I'm going to develop a tourist app, that shows cards in a webview. At first I planned to have variuos html files, next I decided to have a single file html, and each page/card /denoted by a <section> marker.
B4X:
<body>
<section name="page1">
<h1>asdasdasd</h1><p> asd asd sdf sdlfk sdfj sldf j as....
</section>
<section name="page2">
<h1>asdas</h1><p>dasd as....
</section>

I edited the file and transformed it in xhmlt, in order to parse it looking for a section with a certaing name (the idea is taken from cmsimple, a cms that works very well without database and use the <h1> tag as marker for each page.) Then I created that simple piece of code, but realized that the text returned is the last chunk of text enclodes in a set of tag <..> </..>
I thought that the EndElement event for section would be the whole html enclosed inside it.


Is it a correct behaviour? Is there a way to parse a file and to retrieve the text enclosed in side <section name="akey">...</section>?

thanks in advance
alex
the example uses 3 buttons, each looking for a particular section.




B4X:
Sub Process_Globals
Dim parser AsSaxParser
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim FOUND_SEC As Int
Dim key2find AsString
Dim webview1 AsWebView
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("aaaq")
If FirstTime Then
 parser.Initialize
EndIf
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Parser_StartElement (UriAsString, Name AsString, AttributesAsAttributes)
If Name = "section"Then
IfAttributes.GetValue(0)=key2find Then
FOUND_SEC=1
Msgbox(Uri,"")
Else
FOUND_SEC=0
EndIf
EndIf
End Sub
Sub Parser_EndElement (UriAsString, Name AsString, Text AsStringBuilder)
If Name = "section"And FOUND_SEC=1Then
FOUND_SEC=0
 webview1.LoadHtml (Text.ToString)
EndIf
End Sub
Sub retrieve_key( akey AsString)
 key2find=akey
DiminAsInputStream
in = File.OpenInput(File.DirAssets, "index.xml")
parser.Parse(in, "Parser")
in.Close
End Sub
Sub Button2_Click
retrieve_key("ob1.html")
End Sub
Sub Button3_Click
retrieve_key("ob2.html")
End Sub
Sub Button4_Click
retrieve_key("ob3.html")
End Sub

this is the xml file
B4X:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title></title>
 </head>
 <body>
<section name="ob1.html">
 <h1>
 Dante Alighieri
 </h1>
<p align="justify">
 aaaaaa
 </p>
</section>
<section name="ob2.html">
 <h1>
 go Foscolo
 </h1>
<p align="justify">
 bbbb
</p>
</section>
<section name="ob3.html">
 <h1>
 Alessandro Manzoni 
 </h1>
<p align="justify">
cccc
 </p>
</section>
 </body>
</html>

consider that looking for key "ob3.html" my code returns cccc and not
<h1>
Alessandro Manzoni
</h1>
<p align="justify">
cccc
</p>

as I expected

So. I ask if there is way to extraxt a chunk of text using a keyword from a single file containg mode parts corresponding each to an html page.
thanks
 
Top