|
Post by hsfrey on Apr 27, 2008 22:47:11 GMT -5
I read in an xml file and put it in a string. Then I call xmlparser, but get a runtime error message saying "An "xml" declaration is not permitted, except at the beginning of the file" If I move the xmlparser statement to the start of the file, it aborts because the xml string is (obviously) empty. In your xmlparser example, the XML is defined in a DATA string before the start of the code. Is it impossible in RunBasic to read in and then parse XML from a file? Must the XML be included in the program as a DATA string? Or is there a way to declare the xmlparser at the start but define it later in the code? Here's the test code which produces the error message: filename$ = "C:\Computers\Java\Applets\Rulelet\UsingXML\assign.xml" OPEN filename$ for INPUT as #rulefile 'Open file assign.xml for Input with handle #rulefile
allXml$ = "" while not(eof(#rulefile)) INPUT #rulefile, xmlline$ 'Note: line$ was a restricted word, and gave a syntax error allXml$ = allXml$ + " " +xmlline$ WEND CLOSE #rulefile 'print allXml$ 'allXml$ has entire XML file concatenated into one string
xmlparser #accessor, allXml$
print "Contents of <"+#accessor key$()+">" for i = 1 to #accessor elementcount() #c = #accessor #element(i) print "element(" + i + ") "; print #c valueforkey$("pred"); ", "; next i
|
|
|
Post by Alyce on Apr 28, 2008 5:36:44 GMT -5
Is there a reason to read each line separately, the concatenate them back together? You can read the entire file into a string in one statement. Have you seen the Building Blocks on the wiki? Until the next update of RB (with improved helpfile) is released, these can be a big help. runbasic.wikispaces.com/Building+BlocksXML Parser demo: runbasic.wikispaces.com/XmlParserFile access: runbasic.wikispaces.com/Open+Sequential+Files This demo shows how to read data in one hunk with INPUT$(#handle, n) and LOF() from sequential file opened for binary. -------------------------- -------------------------- --------------------------
print:print "Binary Test File 1 - Input$() and LOF()" open DefaultDir$ + "\public\filetest1.txt" for binary as #f fileLen = LOF(#f) a$ = input$(#f, fileLen) print a$ close #f
|
|
|
Post by hsfrey on Apr 28, 2008 13:30:35 GMT -5
Thanks Alyce. That's much better. I was looking for something like that, but the documentation, distributed through multiple files, makes finding things rather difficult.
I've made that change, but, of course, that doesn't help the xmlparser problem.
I still have to read in the file before parsing it, but the error message says that no code can precede the xmlparser statement, (except a data statement.)
Is it true that RB is unable to parse xml read in from a file?
I suppose I could include the xml in a data statement, but I was hoping to have a general program, and not have to reproduce it for every file it uses.
Or, I could write an xml parser myself, but it would be a pity to have to do that.
|
|
|
Post by StefanPendl on Apr 28, 2008 13:47:20 GMT -5
Please check out reply #13 of Parsing XML?I found it by searching the last 700 days for "xml parser".
|
|
|
Post by Alyce on Apr 28, 2008 13:49:31 GMT -5
I still have to read in the file before parsing it, but the error message says that no code can precede the xmlparser statement, (except a data statement.) Is it true that RB is unable to parse xml read in from a file? Since I don't have the file in your code, I modified some of my own code. It allows the user to select a file, then reads it into a variable, then parses it. Be sure to select an XML file. It works for me, showing me that other code can precede an XMLParser statement. I've never experienced the problem you mention. upload "Select a file to upload."; file$
open file$ for binary as #f fileLen = LOF(#f) a$ = input$(#f, fileLen) 'print a$ close #f
xmlparser #parser, a$ print #parser key$() for x = 1 to #parser attribCount() key$ = #parser attribKey$(x) print key$; ", "; print #parser attribValue$(x) next x
(edited to fix typo)
|
|
|
Post by Jerry Muelver on Apr 28, 2008 14:53:33 GMT -5
Without seeing any of the actual file being read, I think the "xml declaration" error refers to the format of the file being read (some <xml> statement out of place), and is an XML rather than RunBASIC error.
|
|
|
Post by hsfrey on Apr 28, 2008 15:49:58 GMT -5
Jerry: Right you are!! ;D I was using the same xml file I'd been parsing in Java, where the xml declaration is required. Sorry to have bothered you all! Harvey
|
|