SAX-WRITER (XML)

Posted by Marjolaine Beaudoin on 16-Dec-2014 15:39

I would like to know how to replace xmlns in WRITE-DATA-ELEMENT. Should I use WRITE-CHARACTERS instead of WRITE-DATA-ELEMENT ? Please see the attached document.

Posted by Peter Judge on 18-Dec-2014 14:24

Does this work? It adds the attributes on the same line as the WRITE-DATA-ELEMENT(). You can change the namespace in the sax-attributes handle too.

	DEF VAR hSAXWr AS HANDLE.
	def var hSaxAttr as handle.
	def var lcXML as longchar.

CREATE SAX-WRITER hSAXWr.
ASSIGN hSAXWr:FORMATTED = TRUE
       hSAXWr:ENCODING  = "UTF-8"
       hSAXWr:STRICT    = FALSE.
       
create sax-attributes hSaxAttr.

hSaxAttr:insert-attribute('prop', 'EXTREF').
       
hSAXWr:SET-OUTPUT-DESTINATION('longchar', lcXML).
hSAXWr:START-DOCUMENT().

hSAXWr:START-ELEMENT('Item').

  hSAXWr:WRITE-DATA-ELEMENT('Dom', 'test', ?, hSaxAttr).  
  hSAXWr:WRITE-DATA-ELEMENT('Dom', 'marjo', ?, hSaxAttr).
  hSaxAttr:update-attribute('prop', 'EXTGO').
hSAXWr:END-ELEMENT('Item').

hSAXWr:END-DOCUMENT().

message 
string(lcxml)
view-as alert-box.


All Replies

Posted by bronco on 17-Dec-2014 00:26

It could look something like this:

 DEF VAR hSAXWr AS HANDLE.

CREATE SAX-WRITER hSAXWr.
ASSIGN hSAXWr:FORMATTED = TRUE
      hSAXWr:ENCODING  = "UTF-8"
      hSAXWr:STRICT    = FALSE.
hSAXWr:SET-OUTPUT-DESTINATION('file', 'c:\temp\bronco.xml').
hSAXWr:START-DOCUMENT().
hSAXWr:START-ELEMENT('Item').
 hSAXWr:START-ELEMENT('Dom').
   hSAXWr:insert-attribute("prop", "EXTREF").
   hSAXWr:write-characters("test").
 hSAXWr:END-ELEMENT('Dom').
 hSAXWr:START-ELEMENT('Dom').
   hSAXWr:insert-attribute("prop", "EXTGO").
   hSAXWr:write-characters("marjo").
 hSAXWr:END-ELEMENT('Dom').
hSAXWr:END-ELEMENT('Item').
hSAXWr:END-DOCUMENT().

Posted by gabriel.lucaciu on 17-Dec-2014 04:06

For more info about adding attributes please check the OpenEdge Documentation:

documentation.progress.com/.../index.html

I think this would help more in achieving the desired behavior.

Posted by Marjolaine Beaudoin on 18-Dec-2014 14:06

Thanks but it's not what I am looking for.  I want the result on the same line ... I can't belive there is no way to do that ...

Posted by Marjolaine Beaudoin on 18-Dec-2014 14:08

Unfortunately, I did ... the prefix is added but somewhere else ... I am pretty sure I am doing something wrong but I can't figure it out.  I think I will open a case.  Thanks

Posted by Peter Judge on 18-Dec-2014 14:24

Does this work? It adds the attributes on the same line as the WRITE-DATA-ELEMENT(). You can change the namespace in the sax-attributes handle too.

	DEF VAR hSAXWr AS HANDLE.
	def var hSaxAttr as handle.
	def var lcXML as longchar.

CREATE SAX-WRITER hSAXWr.
ASSIGN hSAXWr:FORMATTED = TRUE
       hSAXWr:ENCODING  = "UTF-8"
       hSAXWr:STRICT    = FALSE.
       
create sax-attributes hSaxAttr.

hSaxAttr:insert-attribute('prop', 'EXTREF').
       
hSAXWr:SET-OUTPUT-DESTINATION('longchar', lcXML).
hSAXWr:START-DOCUMENT().

hSAXWr:START-ELEMENT('Item').

  hSAXWr:WRITE-DATA-ELEMENT('Dom', 'test', ?, hSaxAttr).  
  hSAXWr:WRITE-DATA-ELEMENT('Dom', 'marjo', ?, hSaxAttr).
  hSaxAttr:update-attribute('prop', 'EXTGO').
hSAXWr:END-ELEMENT('Item').

hSAXWr:END-DOCUMENT().

message 
string(lcxml)
view-as alert-box.


Posted by Marjolaine Beaudoin on 18-Dec-2014 15:35

Thank you very much Peter, it is working perfectly !!!

This thread is closed