ExportXML() missing characters

Just starting out? Need help? Post your questions and find answers here.
ccode
User
User
Posts: 99
Joined: Sat Jun 23, 2018 5:21 pm

Re: [PB5.7 B4] ExportXML() missing characters

Post by ccode »

No idea!

Code: Select all

;????

xml = LoadXML(#PB_Any, "test.xml")

Define size.i = ExportXMLSize(xml)
Debug "xml-FileSize: " + size

Define size2.i = FileSize("test.xml")
Debug "FileSize: " + size2

Define *buffer = AllocateMemory(size2)

If ExportXML(xml, *buffer, size2)

  Debug PeekS(*buffer, -1, #PB_UTF8)

EndIf
FreeMemory(*buffer)
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: [PB5.7 B4] ExportXML() missing characters

Post by #NULL »

I assume 'test.xml' is the snippet from my previous post? I don't think FileSize() makes sense here, but I get your point, i.e. that it works with FileSize()? But it also works with the size from ExportXMLSize() + 8. My guess is that the 5 characters of an entity are counted as 1 for the represented character (CR or LF etc) due to some internal conversion, missing 4 character counts per entity. Two entities makes that 8.

Another thing is that the entities get converted from hexadecimal to decimal in the output. Don't know if that counts as a bug.
ccode
User
User
Posts: 99
Joined: Sat Jun 23, 2018 5:21 pm

Re: [PB5.7 B4] ExportXML() missing characters

Post by ccode »

Hello #NULL !

"test.xml" = It's your example xml text.

Output:
xml-FileSize: 761
FileSize: 772
<?xml version="1.0" encoding="UTF-8"?>
<hixz Source="Living Cookbook 4.0" FileVersion="1.0" date="2013-09-21">
<Cookbooks>
<Cookbook Name="Imported" ID="29"/>
</Cookbooks>
<CookbookChapters>
<CookbookChapter Name="Bisques, Chili, Chowders, Gumbows, Soups & Stews" ID="364" CookbookID="29" ParentChapterID="0"/>
<CookbookChapter Name="Stews *" ID="365" CookbookID="29" ParentChapterID="364"/>
</CookbookChapters>
<Recipes>
<Recipe Name="Chickpea Stew" ID="11007" CookbookID="29" CookbookChapterID="365" Comments="rice.&#13;&#10;Garam masala " Source="Williams-Sonoma" WebPage="http://www.williams-sonoma.com/recipe/c ... CIPESEARCH" CreateDate="2012-02-19" ColorFlag="<None>">
</Recipe>
</Recipes>
</hixz>
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: [PB5.7 B4] ExportXML() missing characters

Post by #NULL »

The file size contains 3 additional bytes for the BOM, plus the 8 missing from ExportXMLSize() makes the difference of 11. (I get a difference of 14 here though, don't know why)
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: [PB5.7 B4] ExportXML() missing characters

Post by NicTheQuick »

Okay, the final version is out but this bug was not fixed. Is there a known workaround for this?
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [PB5.7 B4] ExportXML() missing characters

Post by Little John »

Hi,

using this code from #NULL, I can confirm the problem with PB 5.62 (x64) on Linux Mint 18.3 and on Windows 10, as well with PB 5.70 beta 4 on Windows 10.
NicTheQuick wrote:Is there a known workaround for this?
As a workaround, the following code seems to work here with the above mentioned PB versions and operating systems:

Code: Select all

; slightly changed after <https://www.purebasic.fr/english/viewtopic.php?p=530436#p530436>

EnableExplicit

Define xml.i = LoadXML(#PB_Any, "Recipes.xml")
Define encoding.i = GetXMLEncoding(xml)
Define size.i = ExportXMLSize(xml) + 100
Define *buffer = AllocateMemory(size)

Debug size
Debug ""

If ExportXML(xml, *buffer, size)
   ; ShowMemoryViewer(*buffer, size)
   Debug PeekS(*buffer, -1, encoding)
EndIf
FreeMemory(*buffer)
So I also think that the cause of the problem is a wrong return value of ExportXMLSize().
I guess that using something like

Code: Select all

size = ExportXMLSize(xml) * 2
might be safe for all "normal" XML files.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [PB5.7 B4] ExportXML() missing characters

Post by Fred »

After investigation, the XML you try to parse was not correct (it included an '&' in an attribute while it should be escaped with &amp). After adding the proper error code check, it pointed out the error and that's why your XML was truncated (because everything which is already parsed is kept).

Code: Select all

; slightly changed after <https://www.purebasic.fr/english/viewtopic.php?p=530436#p530436>

Define xml.i = LoadXML(#PB_Any, "crash.xml")

; Display an error message if there was a markup error
;
If XMLStatus(xml) <> #PB_XML_Success
  Message$ = "Error in the XML file:" + Chr(13)
  Message$ + "Message: " + XMLError(xml) + Chr(13)
  Message$ + "Line: " + Str(XMLErrorLine(xml)) + "   Character: " + Str(XMLErrorPosition(xml))
  MessageRequester("Error", Message$)
  End
EndIf

Define encoding.i = GetXMLEncoding(xml)
Define size.i = ExportXMLSize(xml) + 100
Define *buffer = AllocateMemory(size)

Debug size
Debug ""
If ExportXML(xml, *buffer, size)
   ; ShowMemoryViewer(*buffer, size)
   Debug PeekS(*buffer, -1, encoding)
EndIf
FreeMemory(*buffer)
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: ExportXML() missing characters

Post by boddhi »

@Fred

Mieux vaut tard que jamais ! :mrgreen: :lol: :wink:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Post Reply