on 03-04-2014 10:55 AM
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...)
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".
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can use the ActiveX components.
Here are visual basic examples: vb6 - Forcing MSXML to format XML output with indents and newlines - Stack Overflow
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
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
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
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.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ^^
You're right René
Hence, no real straightforward solution.
I also found out a similar CR raised by another customer some time ago:
Jacob
User | Count |
---|---|
83 | |
11 | |
10 | |
8 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.