[newbie] Parsing UTF-8 unicode XML file

Just starting out? Need help? Post your questions and find answers here.
joahim
New User
New User
Posts: 5
Joined: Thu Nov 11, 2010 3:28 pm
Contact:

[newbie] Parsing UTF-8 unicode XML file

Post by joahim »

Hi,

I'm trying to write a simple program for parsing [UTF-8] XML files.
The built-in XML library has some license restrictions, so I decided to read the file step by step, using the command ReadUnicodeCharacter.

Code: Select all

If OpenFile(1,"document.xml")
  While Eof(1) = 0
  something = ReadUnicodeCharacter(1)
  ...  
  Wend  

EndIf
1. How can I create a string from unicode characters read in this way?
2. How can I compare unicode character with a string?

TIA,

joahim
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [newbie] Parsing UTF-8 unicode XML file

Post by infratec »

Hi,

you should read the file like this:

Code: Select all

File = OpenFile(#PB_Any,"document.xml")
If File
  ReadStringFormat(File)  ; to skip a BOM if available
  While Not Eof(File)
    Line$ = ReadString(File, #PB_UTF8)
    Debug Line$
  Wend 
  CloseFile(File)
EndIf
Than you can use the 'normal' string functions like FindString().

Bernd
joahim
New User
New User
Posts: 5
Joined: Thu Nov 11, 2010 3:28 pm
Contact:

Re: [newbie] Parsing UTF-8 unicode XML file

Post by joahim »

ReadStringFormat?

Huh, the XML file contains no EOL character, so the string would have > 1 MB.

Any ideas?

joahim
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [newbie] Parsing UTF-8 unicode XML file

Post by infratec »

Do you have more than 4MB RAM :?:

Than you'll have no problem :mrgreen:

Oh, maybe the Debug takes a bit...
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [newbie] Parsing UTF-8 unicode XML file

Post by infratec »

You can also do this:

Code: Select all

File = OpenFile(#PB_Any,"document.xml")
If File
  ReadStringFormat(File)  ; to skip a BOM if available
  While Not Eof(File)
    Line$ + ReadString(File, #PB_UTF8)
  Wend
  Debug Line$
  CloseFile(File)
EndIf
So you can be sure that always everything is in Line$.

Bernd
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: [newbie] Parsing UTF-8 unicode XML file

Post by coco2 »

Sidenote: joahim how did you post the original post in 1970 :shock:
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [newbie] Parsing UTF-8 unicode XML file

Post by Fred »

joahim wrote:The built-in XML library has some license restrictions, so I decided to read the file step by step, using the command ReadUnicodeCharacter.
Just wondering, which license restriction is bothering you ?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: [newbie] Parsing UTF-8 unicode XML file

Post by IdeasVacuum »

License for the expat XML parser

Copyright (c) 1998, 1999, 2000 Thai Open Source Software Center Ltd
and Clark Cooper
Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 Expat maintainers.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
I cannot see any restrictions?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Zach
Addict
Addict
Posts: 1677
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: [newbie] Parsing UTF-8 unicode XML file

Post by Zach »

Sounds like at most you just have to acknowledge your used the parser in your ReadMe file / documentation, and display the appropriate copyright attributions.
I don't see a problem with this. I'm planning on using it in my Text RPG Engine.
Post Reply