AUTOlisp saves XML file with 0x0 characters

AUTOlisp saves XML file with 0x0 characters

Nejc.Leban
Advocate Advocate
1,367 Views
8 Replies
Message 1 of 9

AUTOlisp saves XML file with 0x0 characters

Nejc.Leban
Advocate
Advocate

Hello!

 

I have a problem with exporting some data from Autocad 2021 to an XML file.

I have a .LSP file which extracts some textual information from the .DWG file and saves it to an XML file which is using UTF-8 encoding. It is supposed to save each value in a separate line and it was working without any problems in Autocad 2018 version.

We've migrated to Autocad 2021 last week and since then, the exporting to XML has changed - each line in XML has a 0x0 hexadecimal value at the end, so when I try to export the XML file to our database, it returns an error, since XML can't have 0x0 values. I've opened the XML file created when Autocad 2018 was still used and the XML when 2021 is used and the only difference is the 0x0 value at the end of each line.

The .LSP file is the same since 2010 so I would like to know what changed with Autocad 2021 that is different from 2018, since 2018 was still working fine?

 

Any help would be greatly appreciated.

Best regards,

Nejc

 

 

0 Likes
Accepted solutions (1)
1,368 Views
8 Replies
Replies (8)
Message 2 of 9

hak_vz
Advisor
Advisor

@Nejc.Leban 

 

Before version 2021 Autolisp didn's support Unicode, here you have changes related to it

 

 0x0 in the byte array, which is considered to be an illegal character in Xml.

 Verify that no character has a hex value greater than 0xFFFD, or less than 0x20.
 Check that the character is not equal to the tab ("t), the newline ("n), the carriage return ("r), or is an invalid XML character below the range of 0x20.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 9

Nejc.Leban
Advocate
Advocate

I really appreciate your explanation and I'm hoping you can help me further.

 

This is a part of the code that creates an XML file with UTF-8 encoding and then writes to it.

 

(setq tobj (vla-getInterfaceObject (vlax-get-acad-object) "ADODB.Stream"))
(setq result (vl-catch-all-apply 'vlax-invoke (list tobj 'Open)))
(setq result (vl-catch-all-apply 'vlax-invoke (list tobj 'Charset "utf-8")))
(setq result (vl-catch-all-apply 'vlax-invoke (list tobj 'SaveToFile xml_file 2)))
(setq result (vl-catch-all-apply 'vlax-invoke (list tobj 'Close)))
(vlax-release-object tobj)
(not (vl-catch-all-error-p result))


(setq f (vla-getInterfaceObject (vlax-get-acad-object) "Scripting.FileSystemObject"))
(setq f_obj (vl-catch-all-apply 'vlax-invoke (list f 'OpenTextFile xml_file 2 0 )))
(setq result (vl-catch-all-apply 'vlax-invoke (list f_obj 'WriteLine "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")))
(setq result (vl-catch-all-apply 'vlax-invoke (list f_obj 'WriteLine "<PrenosPodatki>")))

(setq ccc 0)

 

There are some other lines which access the drawing's title block and then write to the same xml file, but I'm wondering if you could tell me why the two lines above are creating a 0x0 character at the end of each line?

 

The below image shows the xml file open in visual studio where the unwanted characters are visible.

error.png

0 Likes
Message 4 of 9

hak_vz
Advisor
Advisor

The < must be escaped with a &lt; entity, since it is assumed to be the beginning of a tag.

The & must be escaped with a &amp; entity, since it is assumed to be the beginning a entity reference

The > should be escaped with &gt; entity. It is not mandatory -- it depends on the context -- but it is strongly advised to escape it.

The ' should be escaped with a &apos; entity -- mandatory in attributes defined within single quotes but it is strongly advised to always escape it.

The " should be escaped with a &quot; entity -- mandatory in attributes defined within double quotes but it is strongly advised to always escape it.


Now, this can make your xml file legitime but at the other hand it can create problems in furher use of that code if

escaping is not expected.

I'm using Notepad++ as editor and afaik it creates correct utf-8 encoding. Try to open xml file in some app that corectly works with Unicode and save ot there to see if 0x0 characters disapere

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 9

Nejc.Leban
Advocate
Advocate

I'm actually new to this stuff, so I'm not understanding as much as you or I would hope. 🙂 The guy that created the lsp file changed jobs a while ago, so I'm now trying to wrap my head around these things.

 

Do I need to put a &quot before the last three brackets in the code?

Like this?

(setq result (vl-catch-all-apply 'vlax-invoke (list f_obj 'WriteLine "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"&quot)))
(setq result (vl-catch-all-apply 'vlax-invoke (list f_obj 'WriteLine "<PrenosPodatki>"&quot)))

 

I tried opening the xml with Notepad++ and it looks like this:

asd.png

 

 

0 Likes
Message 6 of 9

hak_vz
Advisor
Advisor

@Nejc.Leban 

Will try to replicate this.

Please attach sample xml file.

 

Original code should actually work OK without changes.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 7 of 9

Nejc.Leban
Advocate
Advocate

Thank you. Attached are 2 xml files, 2021 is the one made with Autocad 2021 and the other is made with Autocad 2018.

0 Likes
Message 8 of 9

hak_vz
Advisor
Advisor
Accepted solution

@Nejc.Leban 

Type to system console and run (getvar 'lispsys):

Check this link

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 9 of 9

Nejc.Leban
Advocate
Advocate

@hak_vz 

Thank you so much! I set the value to 0 and it worked. 🙂

I really appreciate you taking the time to help.

 

Best regards,

Nejc