Exception Handling to continue execution

This is a discussion on Exception Handling to continue execution within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to parse an XML file using Libxml2 parser. When I am parsing the file, I get ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Exception Handling to continue execution

    Hi,

    I am trying to parse an XML file using Libxml2 parser. When I am parsing the file, I get the following exception:

    parser error : Input is not proper UTF-8, indicate encoding !

    Bytes: 0xC7 0x41 0x49 0x53 <Name>AMITI…S FRAN╟AISES ANTWERPEN</Name>


    I would want to handle the exception such that the code handles this exception and procedes to the next node for processing without exiting the application.

    Is this possible in C programming?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    In C sure, if you write your own parser.

    But you specified that you are using Libxml2. So you should look at the docs for that to figure out how to do that.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    maybe it is easier just to fix the file - to make it proper xml before parsing. I'm not sure the libxml parser is supposed to partially parse broken xml
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Any suggestions on how to fix the file ? I cannot make out what encoding is used in the XML file. Is there a way to convert the XML file to a standard encoding, say utf-8 without knowing what the actual encoded format is?

    This is imperative as I cannot ascertain the encoding from the XML source.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by dunxton View Post
    Any suggestions on how to fix the file ? I cannot make out what encoding is used in the XML file. Is there a way to convert the XML file to a standard encoding, say utf-8 without knowing what the actual encoded format is?

    This is imperative as I cannot ascertain the encoding from the XML source.
    Have you tried to open file in the textpad and then Save AS... and chose the UTF-8 encoding?

    or Add at the begining of the xml-file string

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    with the correct version and encoding?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    That encoding looks like 0-255 ASCII. The vertical bar before the "AIS" is the bad character - it is not UTF-8. It appears to be extended ASCII, like from the old DOS days. See here: http://www.asciitable.com/
    0XC7 is decimal 199, and that matches the table at that link. (Scroll down to see the high codes).

    edit: Try this code page: 437. http://www.microsoft.com/globaldev/r...e/oem/437.mspx
    Last edited by Dino; 02-02-2009 at 06:28 AM.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    dunxton, why are you having a hard time accepting the fact that it's INVALID XML? Since it's invalid, you can treat it however you want. The best you can do is ask the "3rd party" how they are incorrectly encoding the text so that you can make a best guess as to what the original text was supposed to be. Perhaps it really is just ISO-8859-1 and they are only using the encoding declaration - who knows.

    Or better yet, tell them to fix it!
    http://www.codeguru.com/forum/showthread.php?t=469816

    gg

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Here's an example of how easy this would be to correct with a Java program:
    http://bytes.com/groups/net-c/229845...p437-text-file
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Changing between code-pages and Unicode is one thing (which, yes, is easier in Java). But the only way to "correct" the XML is know how the author incorrectly encoded it. If dunxton (kmajay) is lucky, the author simply used a UTF8 encoding declaration, when they should of used ISO-8859-1.

    ISO-8859-1 code page in Windows (28591): http://www.microsoft.com/globaldev/r...iso/28591.mspx
    Latin Capital Letter C With Cedilla in UTF8 and other code pages: http://www.tachyonsoft.com/uc0000.htm#U00C7

    gg

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Why does the XML have to be corrected? Why wouldn't this work?
    Code:
    <?xml version="1.0" encoding="cp437"?>
    (I guess in answer to my own question "why might it not work", might be because the parser does not recognize that encoding.)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Oooooooh, I get it now. That is supposed to be a C (of some sort). cp437 is not what you want, but probably what Codeplug suggested. So, change the header to read
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    which is a pretty standard codpage, and the parser should have no issue with it.

    Without encoding="..." specified, libxml2 is probably defaulting to UTF-8.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Thanks guys for the help.

    Codeplug, I very well acknowledge the fact that it is an invalid XML. But I am in no position to go back to the Vendor of the XML file for correction. So I was looking for alternative options. But then , as you said, there is probably no other option other than guess the encoding and try.

    But what I dont understand is:

    The input string is "AMITI…S FRAN«AISES ANTWERPEN" . This string has two special characters: and « and both are stored as 2 bytes in UTF-8 and as one byte in iso-8859-1. However, when I parse it, the error is happening only with « and not with . If the parser fails to parse « as UTF-8 then it should fail to parse as well. Why is this not happening?
    Last edited by dunxton; 02-03-2009 at 05:12 AM.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Sounds like a question for the author of the libxml2 library.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  14. #14
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    >> But I am in no position to go back to the Vendor of the XML file for correction.
    What would stop you? Do they not exist anymore?

    >> no other option other than guess
    Yep. Or you could ask the Vender what they're doing. If that truly isn't an option then with a few more data points you could at least make an educated guess.

    >> when I parse it, the error is happening only with « and not with …
    Open up the file in a hex editor. What byte(s) do you see for the … character? Here are the possibilities: http://www.tachyonsoft.com/uc0000.htm#U00C9

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 06:33 AM
  3. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 04:28 PM
  4. is such exception handling approach good?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 07:54 AM
  5. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 01:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21