cancel
Showing results for 
Search instead for 
Did you mean: 

PBDOM : Exporting to XML file gives a single line

Former Member
0 Kudos

I've been starting developing with PowerBuilder 12.5 a few weeks ago. I had to write some XML files, so I got familiar with the PBDOM library.

I can build a lot of different things, it's very nice, but one thing still bothers me :
In the output file, the whole XML is written on a single line.

I use the SaveDocument function.

For example, here is some code :

PBDOM_Document doc
PBDOM_Element noderoot, node1, node11, node12

doc = CREATE PBDOM_Document
doc.NewDocument("NodeRoot")
noderoot = doc.GetRootElement()

node1 = CREATE PBDOM_Element
node1.SetName("Node1")
noderoot.AddContent(node1)
node1.SetAttribute("Attr", "AttrValue")

node11 = CREATE PBDOM_Element
node11.SetName("Node11")
node11.AddContent("Here is a value")
node1.AddContent(node11)

node12 = CREATE PBDOM_ELEMENT
node12.SetName("Node12")
node12.AddContent("Here is another value")
node1.AddContent(node12)

doc.SaveDocument("myDoc.xml")


Here is the result when I open it with notepad++

<NodeRoot><Node1 Attr="AttrValue"><Node11>Here is a value</Node11><Node12>Here is another value</Node12></Node1></NodeRoot>

Whereas I wanted :

<NodeRoot> <Node1 Attr="AttrValue"> <Node11>Here is a value</Node11> <Node12>Here is another value</Node12> </Node1> </NodeRoot>

With the notepad++ XML tools plugin, I can use the "pretty print" function to get this nice representation. But I would like my file to be formatted this way from the beginning. I noticed that the "line ending" was set to UNIX format (indicator on bottom right of the window), but I'm working on Windows. When I use the menu to convert it to Windows format (CR+LF), it changes this indicator, but the code stays on one single line.

Is there a way to tell PBDOM to export the XML file with a nice output ?

Thank you !

Notes :
- Opening the XML file with Internet Explorer or Google Chrome gives me a nice vizualisation, with indentation, line breaks...
- Adding a <?xml version="1.0" encoding="ISO-8859-1" ?> does not help (I've been doing it while exporting some more complex files, but I still get the output on one line...)

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

I don't know an option for pretty output in PBDOM.

You can use the Microsoft XML Core Services. There is a MXXMLWriter class for output. There is a "indent" property for "pretty printing".

Former Member
0 Kudos

Hello,

Thanks for this ! Since PBDOM can export the tree as a string or even a file, maybe it could transmit the output to a MXXMLWriter so it can format it properly.

Now I just have to find out how to use this class in Powerbuilder.

Former Member
0 Kudos

You can use the ActiveX components.

Here are visual basic examples: vb6 - Forcing MSXML to format XML output with indents and newlines - Stack Overflow

Former Member
0 Kudos

I managed to create a MSXML document with an OLE object (not sure of what I did), but these functions look quite heavy, considering that I used PBDOM especially in order to get rid of the writing matters...

Is there a way to use MXXML in Powerbuilder, instead of MSXML ?

I'm new with Powerbuilder, sorry

edit : I managed to use MXXML writer objects in my Powerbuilder code, but am still struggling a bit about writing the file itself. I'll keep you informed

Former Member
0 Kudos

Here is a very simple powerbuilder example. It uses MSXML instead of PBDOM. See my comments for hints how to use it with PBDOM.

oleobject lole_dom, lole_root, lole_element1, lole_element11, lole_element12

oleobject lole_Writer, lole_saxreader

string ls_result

// build DOM (you don't need this if you use PBDOM)

lole_dom = create oleobject

lole_dom.ConnectToNewObject ("Msxml2.DOMDocument")

lole_root = lole_dom.CreateElement ("NodeRoot")

lole_dom.documentElement = lole_root

lole_element1 = lole_dom.CreateElement("Node1")

lole_root.AppendChild(lole_element1)

lole_element1.SetAttribute("Attr", "AttrValue")

lole_element11 = lole_dom.CreateElement("Node11")

lole_element11.AppendChild (lole_dom.CreateTextNode("Here is a value"))

lole_element1.AppendChild(lole_element11)

lole_element12 = lole_dom.CreateElement("Node12")

lole_element12.AppendChild (lole_dom.CreateTextNode("Here is another value"))

lole_element1.AppendChild(lole_element12)

// this saves the DOM without formatting

//lole_dom.save ("d:\testxml.xml")

// this part is for formating

lole_Writer = create oleobject

lole_saxreader = create oleobject

lole_Writer.ConnectToNewObject ("MSXML2.MXXMLWriter")

lole_saxreader.ConnectToNewObject ("MSXML2.SAXXMLReader")

lole_Writer.omitXMLDeclaration = True

lole_Writer.indent = True

lole_saxreader.contentHandler = lole_Writer


// instead of DOM you can also put a XML string to parser: lole_saxreader.parse ("<node>...</node>")

// so you can also use the PBDOM output directly

lole_saxreader.parse (lole_dom)

// here is your formatted output

ls_Result = lole_Writer.output

MessageBox ("", ls_result)

lole_writer.Disconnectobject( )

lole_saxreader.Disconnectobject( )

lole_dom.Disconnectobject( )

DESTROY lole_writer

DESTROY lole_saxreader

DESTROY lole_dom

Former Member
0 Kudos

You rock ! It works !

I had some trouble figuring out how to connect the Reader and the Writer, but now thanks to you I can get a properly indented string.
I can then use it with the PBDOM_Builder to get my final XML file !

Thanks a lot !

Former Member
0 Kudos

By the way, it seems that the lole_Writer.omitXMLDeclaration = True property isn't working... I still get the <?xml version="1.0" encoding="UTF-16LE" standalone="no" ?> line added to the top.

edit : same goes for the .standlone property, which is supposed to hide the standalone part if set to True...

But I can deal with that ! Thanks again

Former Member
0 Kudos

In the documentation of omitXMLDeclaration property you find this:

"The XML declaration is always written if ConformanceLevel is set to Document, even if OmitXmlDeclaration is set to true."

Maybe this is the reasion.

Former Member
0 Kudos

Oh yeah, I think I had seen that but I wondered how to change that ConformanceLevel... And I was pretty tired >.<

edit : damn, I cannot find a way to set that property... It looks like I must set the ConformanceLevel property to Fragment (since it's Document by default), but I can't just type "lole_writer.ConformanceLevel = Fragment", since Powerbuilder doesn't recognize it.
I suppose I should create another OLEObject connected to a XmlWriterSettings object, and somehow attach it to my existing MXXML writer. But how do you know the exact parameter you have to use in the ConnectToNewObject function ?

Moreover, since my object is a MXXMLWriter and not a XmlWriter, can I really access that property ?

I'm gonna check again. Thanks for the tip, you're really saving me here

Former Member
0 Kudos

Sorry, my mistake. You are right, ConformanceLevel ist not for MXXMLWriter.

But for me my example works as expected. Don't know why you get the xml declaration in your output.

Former Member
0 Kudos

No problem, you already helped me a lot. Thanks

edit : I'm stupid, really stupid... The actual result string doesn't have the declaration. It's the PBDOM_Builder that adds the declaration when I build the document from the string. I'm stupid

Answers (1)

Answers (1)

CobyKako
Advisor
Advisor
0 Kudos

Hello Tarek,

According to specification (Sections 2.10 and 2.11 of Extensible Markup Language (XML) 1.0 (Fifth Edition)), XML indentation is not mandatory, hence not implemented by PB built-in PBDOM functions.

I'm not sure if there is a way to force an indentation.

Jacob

Former Member
0 Kudos

Hello,

Thank you for your answer

It's sad but you're right, it's not mandatory. Actually, I'm even able to have a correct validation with a XSD file.

It was just so the customers could take a look at these files just by opening them with the notepad... But I guess then can use a browser too ^^

CobyKako
Advisor
Advisor
0 Kudos

Hi Tarek again,

Check that info:

Formatting the exported XML

HTH

Jacob

Former Member
0 Kudos

Hi Jacob,

this option is only for datawindow export!

CobyKako
Advisor
Advisor
0 Kudos

You're right René

Hence, no real straightforward solution.

I also found out a similar CR raised by another customer some time ago:

Sybase Inc - Search

Jacob

Former Member
0 Kudos

Well at least there is a workaround, which is better than nothing !

Thanks

Former Member
0 Kudos

And maybe I could try loading a XML file or a string into the Datawindow, then export it again from the Datawindow with that option enabled.

I'll try to see if it's possible