[Pharo-project] Is this possible with XMLWriter?
Hi guys. I am using XMLWriter from http://www.squeaksource.com/XMLWriter to write a specific XML that a protocol expects. What I need is to build the XML file from parent to children and dynamically. For example, I want to be able to define a tag, but then, later on, add more tags to a parent element. I found this impossible to do with the current XMLWriter. Here, messages like #tag:with: , #with: do not help. I know I will need to build everything in memory and send a "write contents" at the end to get the final XML string... For example, say I wanna build this XML: <root> <parent1> <child1>grrr<child1> <child2>grrr<child2> </parent1> <parent2> <child3>buhhh<child3> </parent2> </root> i would like an API like this: | xmlWriter | xmlWriter := XMLWriter new. xmlWriter tag: 'root'. (xmlWriter named: 'root') tag: 'parent1'. (xmlWriter named: 'parent1') tag: 'child' with: 'grrr'. (xmlWriter named: 'root') tag: 'parent2'. .... xmlWriter write contents. Or even if I don't have #named: but I can store them in instVars it would be fine: | xmlWriter rootTag parent1Tag | xmlWriter := XMLWriter new. rootTag xmlWriter tag: 'root'. parent1Tag rootTag tag: 'parent1'. parent1Tag tag: 'child' with: 'grrr'. rootTag tag: 'parent2' xmlWriter write contents. Any ideas how to do this? Thanks, -- Mariano http://marianopeck.wordpress.com
Hi Mariano. Some like this: xmlWriter := XMLWriter new writeWith: [ :writer | writer enablePrettyPrinting; xml; comment: 'Generated from Pharo.'. writer tag: 'root' with: [ writer tag: 'parent1' with: [ writer tag: 'child1' with: 'grrr'. writer tag: 'child2' with: 'grrr' ]. writer tag: 'parent2' with: [ writer tag: 'child3' with: 'buhhhr' ] ] ]. xml := xmlWriter contents. 2013/2/27 Mariano Martinez Peck <marianopeck@gmail.com>
Hi guys. I am using XMLWriter from http://www.squeaksource.com/XMLWriterto write a specific XML that a protocol expects. What I need is to build the XML file from parent to children and dynamically. For example, I want to be able to define a tag, but then, later on, add more tags to a parent element. I found this impossible to do with the current XMLWriter. Here, messages like #tag:with: , #with: do not help. I know I will need to build everything in memory and send a "write contents" at the end to get the final XML string...
For example, say I wanna build this XML:
<root> <parent1> <child1>grrr<child1> <child2>grrr<child2> </parent1> <parent2> <child3>buhhh<child3> </parent2> </root>
i would like an API like this:
| xmlWriter | xmlWriter := XMLWriter new. xmlWriter tag: 'root'. (xmlWriter named: 'root') tag: 'parent1'. (xmlWriter named: 'parent1') tag: 'child' with: 'grrr'. (xmlWriter named: 'root') tag: 'parent2'. .... xmlWriter write contents.
Or even if I don't have #named: but I can store them in instVars it would be fine:
| xmlWriter rootTag parent1Tag | xmlWriter := XMLWriter new. rootTag xmlWriter tag: 'root'. parent1Tag rootTag tag: 'parent1'. parent1Tag tag: 'child' with: 'grrr'. rootTag tag: 'parent2' xmlWriter write contents.
Any ideas how to do this?
Thanks,
-- Mariano http://marianopeck.wordpress.com
On Wed, Feb 27, 2013 at 4:24 PM, Gastón Dall' Oglio < gaston.dalloglio@gmail.com> wrote:
Hi Mariano.
Some like this:
xmlWriter := XMLWriter new writeWith: [ :writer | writer enablePrettyPrinting; xml; comment: 'Generated from Pharo.'. writer tag: 'root' with: [ writer tag: 'parent1' with: [ writer tag: 'child1' with: 'grrr'. writer tag: 'child2' with: 'grrr' ]. writer tag: 'parent2' with: [ writer tag: 'child3' with: 'buhhhr' ] ] ]. xml := xmlWriter contents.
Thanks Gaston. But that exactly what I cannot do ;) Looks like I didn't express myself well. In this case, you are defining a tag completely from the beginning. For example, to write parent1 and parent2 you already have to pass them from the beginning to the root. I want to be able to do that at different times. Add one, THEN (not in the same block closure) the other one. Please let me know if my problem is clear. Otherwise, I try again :)
2013/2/27 Mariano Martinez Peck <marianopeck@gmail.com>
Hi guys. I am using XMLWriter from http://www.squeaksource.com/XMLWriterto write a specific XML that a protocol expects. What I need is to build the XML file from parent to children and dynamically. For example, I want to be able to define a tag, but then, later on, add more tags to a parent element. I found this impossible to do with the current XMLWriter. Here, messages like #tag:with: , #with: do not help. I know I will need to build everything in memory and send a "write contents" at the end to get the final XML string...
For example, say I wanna build this XML:
<root> <parent1> <child1>grrr<child1> <child2>grrr<child2> </parent1> <parent2> <child3>buhhh<child3> </parent2> </root>
i would like an API like this:
| xmlWriter | xmlWriter := XMLWriter new. xmlWriter tag: 'root'. (xmlWriter named: 'root') tag: 'parent1'. (xmlWriter named: 'parent1') tag: 'child' with: 'grrr'. (xmlWriter named: 'root') tag: 'parent2'. .... xmlWriter write contents.
Or even if I don't have #named: but I can store them in instVars it would be fine:
| xmlWriter rootTag parent1Tag | xmlWriter := XMLWriter new. rootTag xmlWriter tag: 'root'. parent1Tag rootTag tag: 'parent1'. parent1Tag tag: 'child' with: 'grrr'. rootTag tag: 'parent2' xmlWriter write contents.
Any ideas how to do this?
Thanks,
-- Mariano http://marianopeck.wordpress.com
-- Mariano http://marianopeck.wordpress.com
Yes I suspected that this was what you wanted to say. Regards. 2013/2/27 Mariano Martinez Peck <marianopeck@gmail.com>
On Wed, Feb 27, 2013 at 4:24 PM, Gastón Dall' Oglio < gaston.dalloglio@gmail.com> wrote:
Hi Mariano.
Some like this:
xmlWriter := XMLWriter new writeWith: [ :writer | writer enablePrettyPrinting; xml; comment: 'Generated from Pharo.'. writer tag: 'root' with: [ writer tag: 'parent1' with: [ writer tag: 'child1' with: 'grrr'. writer tag: 'child2' with: 'grrr' ]. writer tag: 'parent2' with: [ writer tag: 'child3' with: 'buhhhr' ] ] ]. xml := xmlWriter contents.
Thanks Gaston. But that exactly what I cannot do ;) Looks like I didn't express myself well. In this case, you are defining a tag completely from the beginning. For example, to write parent1 and parent2 you already have to pass them from the beginning to the root. I want to be able to do that at different times. Add one, THEN (not in the same block closure) the other one.
Please let me know if my problem is clear. Otherwise, I try again :)
2013/2/27 Mariano Martinez Peck <marianopeck@gmail.com>
Hi guys. I am using XMLWriter from http://www.squeaksource.com/XMLWriterto write a specific XML that a protocol expects. What I need is to build the XML file from parent to children and dynamically. For example, I want to be able to define a tag, but then, later on, add more tags to a parent element. I found this impossible to do with the current XMLWriter. Here, messages like #tag:with: , #with: do not help. I know I will need to build everything in memory and send a "write contents" at the end to get the final XML string...
For example, say I wanna build this XML:
<root> <parent1> <child1>grrr<child1> <child2>grrr<child2> </parent1> <parent2> <child3>buhhh<child3> </parent2> </root>
i would like an API like this:
| xmlWriter | xmlWriter := XMLWriter new. xmlWriter tag: 'root'. (xmlWriter named: 'root') tag: 'parent1'. (xmlWriter named: 'parent1') tag: 'child' with: 'grrr'. (xmlWriter named: 'root') tag: 'parent2'. .... xmlWriter write contents.
Or even if I don't have #named: but I can store them in instVars it would be fine:
| xmlWriter rootTag parent1Tag | xmlWriter := XMLWriter new. rootTag xmlWriter tag: 'root'. parent1Tag rootTag tag: 'parent1'. parent1Tag tag: 'child' with: 'grrr'. rootTag tag: 'parent2' xmlWriter write contents.
Any ideas how to do this?
Thanks,
-- Mariano http://marianopeck.wordpress.com
-- Mariano http://marianopeck.wordpress.com
2013/2/27 Gastón Dall' Oglio <gaston.dalloglio@gmail.com>:
Yes I suspected that this was what you wanted to say.
If I understood what he wants, the thing is that XMLWriter, as also is its Seaside ancestor, are stream oriented. To do what he wants he should have an XML DOM and manipulate the DOM nodes directly. I don't know if we have such thing in Pharo. Regards, E.
Exactly. I guess that it is not possibly add more child nodes at one node later, because in canvas-brush pattern when you pass a message like #with: to the actual node (a brush) its contents is written inmediately in the underlying stream of characters (within canvas), thus if at continuation you add another siblings nodes of first one, its contents will be also written in the stream immediately, and you no longer have a chance to add more children to the first node. 2013/2/27 Esteban A. Maringolo <emaringolo@gmail.com>
2013/2/27 Gastón Dall' Oglio <gaston.dalloglio@gmail.com>:
Yes I suspected that this was what you wanted to say.
If I understood what he wants, the thing is that XMLWriter, as also is its Seaside ancestor, are stream oriented. To do what he wants he should have an XML DOM and manipulate the DOM nodes directly. I don't know if we have such thing in Pharo.
Regards,
E.
On 27 Feb 2013, at 21:18, "Esteban A. Maringolo" <emaringolo@gmail.com> wrote:
To do what he wants he should have an XML DOM and manipulate the DOM nodes directly. I don't know if we have such thing in Pharo.
I would be very surprised if that were not possible: create a DOM tree, either manually or by parsing some XML, then add some nodes and finally render it again. I never did this, but I'll have a look if I have some time. Sven -- Sven Van Caekenberghe http://stfx.eu Smalltalk is the Red Pill
+1 Mariano I'm about to migrate the XMLParser to SmalltalkHub and I know that you have a DOMTree and a saxAPI So with the DOMTree I do not see why we could not manipulate the tree. Stef
On 27 Feb 2013, at 21:18, "Esteban A. Maringolo" <emaringolo@gmail.com> wrote:
To do what he wants he should have an XML DOM and manipulate the DOM nodes directly. I don't know if we have such thing in Pharo.
I would be very surprised if that were not possible: create a DOM tree, either manually or by parsing some XML, then add some nodes and finally render it again.
I never did this, but I'll have a look if I have some time.
Sven
-- Sven Van Caekenberghe http://stfx.eu Smalltalk is the Red Pill
ups! I feel an idiot. The problem was trying to do these manipulations with XMLWriter class instead of the DOM itself. 2013/2/27 stephane ducasse <stephane.ducasse@free.fr>
+1
Mariano I'm about to migrate the XMLParser to SmalltalkHub and I know that you have a DOMTree and a saxAPI So with the DOMTree I do not see why we could not manipulate the tree.
Stef
On 27 Feb 2013, at 21:18, "Esteban A. Maringolo" <emaringolo@gmail.com>
wrote:
To do what he wants he should have an XML DOM and manipulate the DOM nodes directly. I don't know if we have such thing in Pharo.
I would be very surprised if that were not possible: create a DOM tree,
either manually or by parsing some XML, then add some nodes and finally render it again.
I never did this, but I'll have a look if I have some time.
Sven
-- Sven Van Caekenberghe http://stfx.eu Smalltalk is the Red Pill
On 27 Feb 2013, at 22:17, stephane ducasse <stephane.ducasse@free.fr> wrote:
+1
Mariano I'm about to migrate the XMLParser to SmalltalkHub and I know that you have a DOMTree and a saxAPI So with the DOMTree I do not see why we could not manipulate the tree.
Stef
Here are two approaches that seem to work fine: | dom | dom := (XMLDOMParser parse: '<foo><bar>text</bar><baz>100</baz></foo>'). dom firstElement addNode: ((XMLElement named: 'mariano') addContent: 'Hi there !' ). dom prettyPrinted. | xmlWriter dom | xmlWriter := XMLWriter new writeWith: [ :writer | writer enablePrettyPrinting; xml; comment: 'Generated from Pharo.'. writer tag: 'root' with: [ writer tag: 'parent1' with: [ writer tag: 'child1' with: 'grrr'. writer tag: 'child2' with: 'grrr' ]. writer tag: 'parent2' with: [ writer tag: 'child3' with: 'buhhhr' ] ] ]. dom := (XMLDOMParser parse: xmlWriter contents). dom firstElement addNode: ((XMLElement named: 'mariano') addContent: 'Hi there !' ). dom prettyPrinted. I guess it would also be possible to completely construct the DOM tree manually. Regards, Sven
On 27 Feb 2013, at 21:18, "Esteban A. Maringolo" <emaringolo@gmail.com> wrote:
To do what he wants he should have an XML DOM and manipulate the DOM nodes directly. I don't know if we have such thing in Pharo.
I would be very surprised if that were not possible: create a DOM tree, either manually or by parsing some XML, then add some nodes and finally render it again.
I never did this, but I'll have a look if I have some time.
Sven
-- Sven Van Caekenberghe http://stfx.eu Smalltalk is the Red Pill
And in case anyone is not aware, this video shows how to manipulate xml: http://www.pharocasts.com/2010/08/see-how-to-get-data-from-url-parse-xml.htm... 2013/2/27 Sven Van Caekenberghe <sven@stfx.eu>
On 27 Feb 2013, at 22:17, stephane ducasse <stephane.ducasse@free.fr> wrote:
+1
Mariano I'm about to migrate the XMLParser to SmalltalkHub and I know that you have a DOMTree and a saxAPI So with the DOMTree I do not see why we could not manipulate the tree.
Stef
Here are two approaches that seem to work fine:
| dom | dom := (XMLDOMParser parse: '<foo><bar>text</bar><baz>100</baz></foo>'). dom firstElement addNode: ((XMLElement named: 'mariano') addContent: 'Hi there !' ). dom prettyPrinted.
| xmlWriter dom | xmlWriter := XMLWriter new writeWith: [ :writer | writer enablePrettyPrinting; xml; comment: 'Generated from Pharo.'. writer tag: 'root' with: [ writer tag: 'parent1' with: [ writer tag: 'child1' with: 'grrr'. writer tag: 'child2' with: 'grrr' ]. writer tag: 'parent2' with: [ writer tag: 'child3' with: 'buhhhr' ] ] ]. dom := (XMLDOMParser parse: xmlWriter contents). dom firstElement addNode: ((XMLElement named: 'mariano') addContent: 'Hi there !' ). dom prettyPrinted.
I guess it would also be possible to completely construct the DOM tree manually.
Regards,
Sven
On 27 Feb 2013, at 21:18, "Esteban A. Maringolo" <emaringolo@gmail.com> wrote:
To do what he wants he should have an XML DOM and manipulate the DOM nodes directly. I don't know if we have such thing in Pharo.
I would be very surprised if that were not possible: create a DOM tree, either manually or by parsing some XML, then add some nodes and finally render it again.
I never did this, but I'll have a look if I have some time.
Sven
-- Sven Van Caekenberghe http://stfx.eu Smalltalk is the Red Pill
Please use the version that is now published in PharoExtras. If you add tests please commit them :) On Feb 27, 2013, at 6:55 PM, Mariano Martinez Peck <marianopeck@gmail.com> wrote:
Hi guys. I am using XMLWriter from http://www.squeaksource.com/XMLWriter to write a specific XML that a protocol expects. What I need is to build the XML file from parent to children and dynamically. For example, I want to be able to define a tag, but then, later on, add more tags to a parent element. I found this impossible to do with the current XMLWriter. Here, messages like #tag:with: , #with: do not help. I know I will need to build everything in memory and send a "write contents" at the end to get the final XML string...
For example, say I wanna build this XML:
<root> <parent1> <child1>grrr<child1> <child2>grrr<child2> </parent1> <parent2> <child3>buhhh<child3> </parent2> </root>
i would like an API like this:
| xmlWriter | xmlWriter := XMLWriter new. xmlWriter tag: 'root'. (xmlWriter named: 'root') tag: 'parent1'. (xmlWriter named: 'parent1') tag: 'child' with: 'grrr'. (xmlWriter named: 'root') tag: 'parent2'. .... xmlWriter write contents.
Or even if I don't have #named: but I can store them in instVars it would be fine:
| xmlWriter rootTag parent1Tag | xmlWriter := XMLWriter new. rootTag xmlWriter tag: 'root'. parent1Tag rootTag tag: 'parent1'. parent1Tag tag: 'child' with: 'grrr'. rootTag tag: 'parent2' xmlWriter write contents.
Any ideas how to do this?
Thanks,
-- Mariano http://marianopeck.wordpress.com
Thank you all guys. Indeed, it was quite possible to manually build the DOM. I was confused because the package was called "XML*Parser*". I could see the nodes and all the DOM but I didn't expected these classes would understand "writing behavior" like #named: , #add: etc... but yes, they do understand so you can manually build the DOM and when you are done you can just get the resulting string. Best, On Thu, Feb 28, 2013 at 12:50 PM, stephane ducasse <stephane.ducasse@free.fr
wrote:
Please use the version that is now published in PharoExtras. If you add tests please commit them :)
On Feb 27, 2013, at 6:55 PM, Mariano Martinez Peck <marianopeck@gmail.com> wrote:
Hi guys. I am using XMLWriter from http://www.squeaksource.com/XMLWriterto write a specific XML that a protocol expects. What I need is to build the XML file from parent to children and dynamically. For example, I want to be able to define a tag, but then, later on, add more tags to a parent element. I found this impossible to do with the current XMLWriter. Here, messages like #tag:with: , #with: do not help. I know I will need to build everything in memory and send a "write contents" at the end to get the final XML string...
For example, say I wanna build this XML:
<root> <parent1> <child1>grrr<child1> <child2>grrr<child2> </parent1> <parent2> <child3>buhhh<child3> </parent2> </root>
i would like an API like this:
| xmlWriter | xmlWriter := XMLWriter new. xmlWriter tag: 'root'. (xmlWriter named: 'root') tag: 'parent1'. (xmlWriter named: 'parent1') tag: 'child' with: 'grrr'. (xmlWriter named: 'root') tag: 'parent2'. .... xmlWriter write contents.
Or even if I don't have #named: but I can store them in instVars it would be fine:
| xmlWriter rootTag parent1Tag | xmlWriter := XMLWriter new. rootTag xmlWriter tag: 'root'. parent1Tag rootTag tag: 'parent1'. parent1Tag tag: 'child' with: 'grrr'. rootTag tag: 'parent2' xmlWriter write contents.
Any ideas how to do this?
Thanks,
-- Mariano http://marianopeck.wordpress.com
-- Mariano http://marianopeck.wordpress.com
mariano I migrated Pastell which is a library to manipulate XML to PharoExtras. Have a look Stef On Mar 5, 2013, at 3:27 PM, Mariano Martinez Peck <marianopeck@gmail.com> wrote:
Thank you all guys. Indeed, it was quite possible to manually build the DOM. I was confused because the package was called "XMLParser". I could see the nodes and all the DOM but I didn't expected these classes would understand "writing behavior" like #named: , #add: etc... but yes, they do understand so you can manually build the DOM and when you are done you can just get the resulting string.
Best,
On Thu, Feb 28, 2013 at 12:50 PM, stephane ducasse <stephane.ducasse@free.fr> wrote: Please use the version that is now published in PharoExtras. If you add tests please commit them :)
On Feb 27, 2013, at 6:55 PM, Mariano Martinez Peck <marianopeck@gmail.com> wrote:
Hi guys. I am using XMLWriter from http://www.squeaksource.com/XMLWriter to write a specific XML that a protocol expects. What I need is to build the XML file from parent to children and dynamically. For example, I want to be able to define a tag, but then, later on, add more tags to a parent element. I found this impossible to do with the current XMLWriter. Here, messages like #tag:with: , #with: do not help. I know I will need to build everything in memory and send a "write contents" at the end to get the final XML string...
For example, say I wanna build this XML:
<root> <parent1> <child1>grrr<child1> <child2>grrr<child2> </parent1> <parent2> <child3>buhhh<child3> </parent2> </root>
i would like an API like this:
| xmlWriter | xmlWriter := XMLWriter new. xmlWriter tag: 'root'. (xmlWriter named: 'root') tag: 'parent1'. (xmlWriter named: 'parent1') tag: 'child' with: 'grrr'. (xmlWriter named: 'root') tag: 'parent2'. .... xmlWriter write contents.
Or even if I don't have #named: but I can store them in instVars it would be fine:
| xmlWriter rootTag parent1Tag | xmlWriter := XMLWriter new. rootTag xmlWriter tag: 'root'. parent1Tag rootTag tag: 'parent1'. parent1Tag tag: 'child' with: 'grrr'. rootTag tag: 'parent2' xmlWriter write contents.
Any ideas how to do this?
Thanks,
-- Mariano http://marianopeck.wordpress.com
-- Mariano http://marianopeck.wordpress.com
participants (5)
-
Esteban A. Maringolo -
Gastón Dall' Oglio -
Mariano Martinez Peck -
stephane ducasse -
Sven Van Caekenberghe