SAX-WRITER (XML)

Posted by Marjolaine Beaudoin on 21-Jan-2016 08:47

Hi,

I need to add an 'empty' tag on my xml file (using SAX-WRITER of course).

What I want :

<element></element> (no space between the tag). 

I do not want :

<element/> or <element> </element> (space between the tag).

I tried to use WRITE-EMPTY-ELEMENT and WRITE-DATA-ELEMENT but it did not work.  START-DOCUMENT and END-DOCUMENT add a space between the thag (and I don't want any space).

I hope someone could help !

Thanks,

Marjolaine

Posted by Marjolaine Beaudoin on 21-Jan-2016 10:02

Hi All,

Thank you very much for time ... it seems it is 11.2 that does not work correctly.  I just tried in 10.2b and 11.3 and it works fine ...

Unfortunately, my client is in 11.2 so I really don't know what to do ...

Again thank you,

Marjolaine

All Replies

Posted by Alex Herbstritt on 21-Jan-2016 08:58

You can do this by using START-ELEMENT and END-ELEMENT with no content in between them.

Here is a code snippet:

define variable swh as handle no-undo.

create sax-writer swh.

swh:set-output-destination("FILE", "emptyelement.xml").

swh:start-document().

swh:start-element("root").

swh:start-element("empty").

swh:end-element("empty").

swh:end-element("root").

swh:end-document().

And here is the output. I think that it has the empty element that you desire.

<?xml version="1.0" encoding="ISO-8859-1"?>

.

Posted by Frank Meulblok on 21-Jan-2016 09:00

Can't answer the immediate question off the top of my head. However, when you say "I do not want : <element/>",

I can't resist asking: Why not ?

For any XML parser that's standards-compliant, "<element/>" and "<element></element>" are equivalent, with the first often being preferred as it results in a smaller document size.

Posted by Piotr Ryszkiewicz on 21-Jan-2016 09:01

Hi,

That's weird... for me WRITE-DATA-ELEMENT works as expected.

hSAXWriter:WRITE-DATA-ELEMENT("element", "")

gives me   <element></element> (no space).

OE 11.3.3 on Windows.

Are you sure you are sending empty string, not space character?

Regards,

P.

Posted by AdrianJones on 21-Jan-2016 09:30

fyi hSAXWriter:WRITE-DATA-ELEMENT("element", "") works for me too (no space) on 10.2b windows.

Posted by Alex Herbstritt on 21-Jan-2016 09:39

Yes, I am seeing the same. Even with

swh:formatted = true.

I am getting no space in the empty element. This does not happen with my code above.

<?xml version="1.0"?>
<root>
  <empty>
  </empty>
  <emptydataelement></emptydataelement>
</root>

Posted by Marjolaine Beaudoin on 21-Jan-2016 09:43

My file is rejected by the Financial institution ... they don't like it

Posted by Marjolaine Beaudoin on 21-Jan-2016 09:44

DEF VAR hSAXWr AS HANDLE.

CREATE SAX-WRITER hSAXWr.

ASSIGN hSAXWr:FORMATTED = TRUE

      hSAXWr:ENCODING  = "ISO-8859-1"

      hSAXWr:STRICT    = TRUE.

hSAXWr:SET-OUTPUT-DESTINATION('file', 'marjo.xml').

hSAXWr:START-DOCUMENT().

hSAXWr:WRITE-DATA-ELEMENT('CATI', ''). /**/

hSAXWr:END-DOCUMENT().

This is the result for me (11.3)

<?xml version="1.0" encoding="ISO-8859-1"?>

<CATI/>

Posted by Marjolaine Beaudoin on 21-Jan-2016 09:45

Unfortunately, a space is added between the tag :(

Posted by Marjolaine Beaudoin on 21-Jan-2016 09:49

Sorry, I am in 11.2 ... If I use your code ... I have <element/> as result ... Why is this happening to me :(

Posted by Marjolaine Beaudoin on 21-Jan-2016 10:02

Hi All,

Thank you very much for time ... it seems it is 11.2 that does not work correctly.  I just tried in 10.2b and 11.3 and it works fine ...

Unfortunately, my client is in 11.2 so I really don't know what to do ...

Again thank you,

Marjolaine

Posted by Piotr Ryszkiewicz on 21-Jan-2016 10:10

If upgrade to 11.3 is not an option, then maybe you could process xml file after you create it outside ABL.

Probably the easiest way would be to put some very specific string where you finally want empty element and then use sed to replace it with empty string.

Posted by GregHiggins on 21-Jan-2016 10:12

Since you are using the sax-writer, I would write to a longchar and then use

assign

 lc = replace ( lc, "<empty />", "<empty></empty>" )

 .

Posted by Piotr Ryszkiewicz on 21-Jan-2016 10:18

Greg,

That would work as well, but you would have to perform replace for every element name that could theoretically generate empty element. If the file structure is complicated and the file is large, it wouldn't be easy nor fast.

Regards,

Piotr

Posted by Marjolaine Beaudoin on 21-Jan-2016 10:19

Well ... I think I will try that  : hSAXWr:WRITE-CHARACTERS('<CATI></CATI>').   I never use that before but I think it will work ... I will test it with the Financial institution and see what will happen.  Thank you !

Posted by GregHiggins on 21-Jan-2016 10:33

Well I also agree with your answer, and replacing "###DELETEME###" with "" is also an acceptable solution to me.  I only consider fast an issue when we are talking people time (or hours of difference) and we obviously aren't, and as for easy, that is a matter of opinion. You threw sed into the mix without a thought for whether it was even in the user's experience; I was trying to stay within a realm the user could be expected to understand.

Posted by Piotr Ryszkiewicz on 21-Jan-2016 10:42

I am not sure if I understand you correctly... I did not mean that enduser should use sed to replace strings of course. The application should automatically call it after xml is generated. But Marjolaine already has different solution, so this is theoretical discussion only :)

Posted by Marian Edu on 21-Jan-2016 11:37

That will escape the text so not quite what you are looking for, go for `write-fragment` instead but it's your responsibility to put valid xml in :)

Posted by Alex Herbstritt on 21-Jan-2016 12:21

I just ran your example code through 11.2 and am getting the same results as with all other versions: "<CATI></CATI>" with no space between.

Looking through our code base, no changes were made to the SAX-WRITER in 11.2.

Are you positive that there is nothing wrong with your source file that is running in 11.2?

Posted by Marjolaine Beaudoin on 21-Jan-2016 12:36

Hi Alex,

I really don't know what is happening to me.  It is not the first time I use the SAX-WRITER and I never had that kind of issue.  I really can't figure why it is not working (I feel so dumb).

I always have : <CATI/> ... I ask one of my collegue (who works on 10.2b) to run my example (the one I put earlier) and it works perfectly.

I gave up ... so I use WRITE-CHARACTERS instead (I don't like it ... it's weird when you look at the xml file but it is the only way I found) ...

Anytime for a webex conference ... I can show you (but don't botter,  I know how time is precious)

Marjolaine

Posted by Marjolaine Beaudoin on 21-Jan-2016 12:43

Oups Marian,

I did not see your post ... I will try WRITE-FRAGMENT instead of WRITE-CHARACTERS ...

I hope one of these will work

Thanks,

Marjolaine

Posted by Marjolaine Beaudoin on 21-Jan-2016 13:11

Wow ... It is entirely my fault.  I did not realize I was the one who alterates the file by doing my tests.  Shame on me.  Everything works fine.

I am so sorry ... I waste your time (and mine as well)

Thanks Alex ... when you told me about my source file ... I double check and you were right ...

This thread is closed