[Twisted-Python] regarding xml elements
Hi, I have an xml file in my application, I have created an element using Example goes like this......... document.createElement("abc") and i appeneded it by using append() method. But how i can reflect this change to my xml file? and one more thing is i want to create element with some other parameters.... like............. <abc m=" " n=" "> </abc> and i have m and n values as strings with me. can anybody help me to create this element and write it to the existing xml file as a child of an existing element? Thanks in advance.. Raj.. Forgot the famous last words? Access your message archive online at http://in.messenger.yahoo.com/webmessengerpromo.php
Raj, I think you are on the wrong list. I'm going out on a limb here, but I think you want to communicate from a Javascript application to a server. I might suggest using Dojo for this (http://dojotoolkit.org), which should work if you are trying to write the entire XML string back to the server, and might should (I live in the south) work to mirror the xml manipulations to equivalent functions on the server. Did you consider JSON instead of XML? I have not used Dojo outside of the US and there are potential performance issues elsewhere due to the large libraries cached by AOL. My apologies if I am off track. George On Fri, 2008-03-28 at 17:14 +0530, Raj kumar wrote:
Hi, I have an xml file in my application, I have created an element using Example goes like this.........
document.createElement("abc") and i appeneded it by using append() method. But how i can reflect this change to my xml file? and one more thing is i want to create element with some other parameters.... like..............
<abc m=" " n=" "> </abc> and i have m and n values as strings with me.. can anybody help me to create this element and write it to the existing xml file as a child of an existing element?
Thanks in advance...
Raj..
______________________________________________________________________ Meet people who discuss and share your passions. Join them now. _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python -- George Pauly Ring Development www.ringdevelopment.com
On Mar 28, 2008, at 7:44 AM, Raj kumar wrote:
Hi, I have an xml file in my application, I have created an element using Example goes like this.........
document.createElement("abc") and i appeneded it by using append() method. But how i can reflect this change to my xml file? and one more thing is i want to create element with some other parameters.... like..............
<abc m=" " n=" "> </abc> and i have m and n values as strings with me.. can anybody help me to create this element and write it to the existing xml file as a child of an existing element?
It sounds like you're working with t.w.microdom. I believe you'll want to use Document.writexml(). It's not documented in the code, and I haven't looked anywhere else, but you should be able to use it like this: def writexml(self, stream, indent='', addindent='', newl='', strip=0, nsprefixes={}, namespace=''): You'll probably want to ignore most of the parameters. I'm fairly sure indent, addindent,newl, and strip are related to pretty-printing. I've never used the namespace-related vars either, so I can't really tell you what they do ;-)... Also, in case it's not obvious, 'stream' should be a file object of some kind. To add attributes to your element, you can invoke createElement like this: document.createElement('abc', attributes={'m':'mvalue', 'n':'nvalue'}) Then just append() it normally as you've already done. -phil
On Fri, 28 Mar 2008 10:32:44 -0400, Phil Christensen <phil@bubblehouse.org> wrote:
On Mar 28, 2008, at 7:44 AM, Raj kumar wrote:
Hi, I have an xml file in my application, I have created an element using Example goes like this.........
document.createElement("abc") and i appeneded it by using append() method. But how i can reflect this change to my xml file? and one more thing is i want to create element with some other parameters.... like..............
<abc m=" " n=" "> </abc> and i have m and n values as strings with me.. can anybody help me to create this element and write it to the existing xml file as a child of an existing element?
It sounds like you're working with t.w.microdom.
If so, stop. Don't use twisted.web.microdom. There are plenty of better XML libraries for Python. Jean-Paul
On Mar 28, 2008, at 10:45 AM, Jean-Paul Calderone wrote:
On Fri, 28 Mar 2008 10:32:44 -0400, Phil Christensen <phil@bubblehouse.org
wrote: On Mar 28, 2008, at 7:44 AM, Raj kumar wrote:
Hi, I have an xml file in my application, I have created an element using Example goes like this.........
document.createElement("abc")
It sounds like you're working with t.w.microdom.
If so, stop.
Don't use twisted.web.microdom. There are plenty of better XML libraries for Python.
What's wrong with it? It's got a familiar API, and it's easier to work with than the built-in expat parser. If you've already got a dependency on Twisted, it seems like a good choice unless you need a high level of performance. If it's so bad that you'd tell someone not to use it, why isn't it deprecated? -phil
On Fri, 28 Mar 2008 10:51:10 -0400, Phil Christensen <phil@bubblehouse.org> wrote:
On Mar 28, 2008, at 10:45 AM, Jean-Paul Calderone wrote:
On Fri, 28 Mar 2008 10:32:44 -0400, Phil Christensen <phil@bubblehouse.org
wrote: On Mar 28, 2008, at 7:44 AM, Raj kumar wrote:
Hi, I have an xml file in my application, I have created an element using Example goes like this.........
document.createElement("abc") It sounds like you're working with t.w.microdom.
If so, stop.
Don't use twisted.web.microdom. There are plenty of better XML libraries for Python.
What's wrong with it? It's got a familiar API, and it's easier to work with than the built-in expat parser. If you've already got a dependency on Twisted, it seems like a good choice unless you need a high level of performance.
It's full of bugs that we probably won't fix, even if you tell us about them.
If it's so bad that you'd tell someone not to use it, why isn't it deprecated?
Because it would take a lot of work to deprecate, and there are a lot of other things people are working on that are deemed more important. Jean-Paul
On 02:55 pm, exarkun@divmod.com wrote:
On Fri, 28 Mar 2008 10:51:10 -0400, Phil Christensen <phil@bubblehouse.org> wrote:
If it's so bad that you'd tell someone not to use it, why isn't it deprecated?
Because it would take a lot of work to deprecate, and there are a lot of other things people are working on that are deemed more important.
Also because there isn't a complete consensus on how terrible it is. If you are doing advanced XML processing, it definitely isn't the best choice, because it's missing a bunch of features and it has some idiosyncratic behavior. However, it is conveniently available without adding another dependency and it supports simple use-cases OK; this is still a problem with other XML libraries in Python, especially DOM libraries. (For example, incompatibilities between the various lxml implementations.) I, personally, don't think it should be deprecated, even if it shouldn't be particularly strongly recommended, either.
On Fri, 28 Mar 2008 22:59:21 -0000, glyph@divmod.com wrote:
On 02:55 pm, exarkun@divmod.com wrote:
On Fri, 28 Mar 2008 10:51:10 -0400, Phil Christensen <phil@bubblehouse.org> wrote:
If it's so bad that you'd tell someone not to use it, why isn't it deprecated?
Because it would take a lot of work to deprecate, and there are a lot of other things people are working on that are deemed more important.
Also because there isn't a complete consensus on how terrible it is. If you are doing advanced XML processing, it definitely isn't the best choice, because it's missing a bunch of features and it has some idiosyncratic behavior. However, it is conveniently available without adding another dependency and it supports simple use-cases OK; this is still a problem with other XML libraries in Python, especially DOM libraries. (For example, incompatibilities between the various lxml implementations.)
Here's an example of a behavior it has which probably isn't going to change any time soon: >>> from twisted.web.microdom import parseString >>> s = '<div><span>hello</span> <span>world</span></div>' >>> parseString(s).toxml() '<?xml version="1.0"?><div><span>hello</span><span>world</span></div>' >>> So if you need such advanced XML features as correct whitespace handling, steer clear. ;) Jean-Paul
On Mar 28, 2008, at 7:33 PM, Jean-Paul Calderone wrote:
On Fri, 28 Mar 2008 22:59:21 -0000, glyph@divmod.com wrote:
On 02:55 pm, exarkun@divmod.com wrote:
On Fri, 28 Mar 2008 10:51:10 -0400, Phil Christensen <phil@bubblehouse.org> wrote:
If it's so bad that you'd tell someone not to use it, why isn't it deprecated?
Because it would take a lot of work to deprecate, and there are a lot of other things people are working on that are deemed more important.
Also because there isn't a complete consensus on how terrible it is. If you are doing advanced XML processing, it definitely isn't the best choice, because it's missing a bunch of features and it has some idiosyncratic behavior. However, it is conveniently available without adding another dependency and it supports simple use-cases OK; this is still a problem with other XML libraries in Python, especially DOM libraries. (For example, incompatibilities between the various lxml implementations.)
Here's an example of a behavior it has which probably isn't going to change any time soon:
from twisted.web.microdom import parseString s = '<div><span>hello</span> <span>world</span></div>' parseString(s).toxml() '<?xml version="1.0"?><div><span>hello</span><span>world</span></ div>'
So if you need such advanced XML features as correct whitespace handling, steer clear. ;)
I have to say, I don't find this to be that big an issue. I think if you're using XML as a data interchange format (as I know the original poster was), whitespace is generally syntactically meaningless. In fact, in my particular usage, I'm dealing with SAML responses which need to have a signature verified using xmlsec, and any whitespace like that would break the signature. I know there's a tendency among developers to immediately disregard something that is less than perfect, but I like microdom too much to give up on it yet ;-)... -phil
On 12:56 am, phil@bubblehouse.org wrote:
On Mar 28, 2008, at 7:33 PM, Jean-Paul Calderone wrote:
On Fri, 28 Mar 2008 22:59:21 -0000, glyph@divmod.com wrote:
On 02:55 pm, exarkun@divmod.com wrote:
On Fri, 28 Mar 2008 10:51:10 -0400, Phil Christensen <phil@bubblehouse.org> wrote:
from twisted.web.microdom import parseString s = '<div><span>hello</span> <span>world</span></div>' parseString(s).toxml() '<?xml version="1.0"?><div><span>hello</span><span>world</span></ div>'
So if you need such advanced XML features as correct whitespace handling, steer clear. ;)
I have to say, I don't find this to be that big an issue. I think if you're using XML as a data interchange format (as I know the original poster was), whitespace is generally syntactically meaningless.
Like many things in Microdom, whitespace handling does not strive to be particularly spec-compliant (the spec does say "An XML processor MUST always pass all characters in a document that are not markup through to the application."), but to be useful for simple cases and stable enough that your code won't break. If you want whitespace you can probably cram it in there. For example, it has a creative misinterpretation of the "xml:space" attribute:
from twisted.web.microdom import parseString s = '<div xml:space="preserve"><span>hello</span> <span>world</span></div>' parseString(s).toxml() '<?xml version="1.0"?><div xml:space="preserve"><span>hello</span> <span>world</span></div>'
It is also hard-coded to preserve space in <pre> tags, which is also broken because it doesn't really honor namespaces, and therefore has no idea if your document is HTML or not, and it can't read DTDs so it doesn't know if your elements have this attribute set implicitly (and so on and so on). This could be made into *slightly* less of a hack with a preserveSpace argument to parse*(), of course; the implementation would probably be very straightforward (c.f. MicroDOMParser.shouldPreserveSpace). Maybe someone who actually likes Microdom, such as Phil, will add one, since all I'm committing to here is not totally hating it ;).
Quick question about (sub)processes in twisted : how bad is it to use the subprocess module ? ( in general / in this application ) I have the following setup : - Twisted Daemon that does 5 things (very well!) - A new requirement -- to execute 2 scripts on the commandline , one after the other ( ie, blocking ) the 2 commandline scripts are just tossing some PostgreSQL commands via `postgresql db < file.txt` i was looking at reactor.SpawnProcess -- which I know is more correct. it just seems to be a bit annoying to use , and I know the subprocess module already.
On Sep 26, 2013, at 6:51 PM, Jonathan Vanasco <twisted-python@2xlp.com> wrote:
Quick question about (sub)processes in twisted :
how bad is it to use the subprocess module ? ( in general / in this application )
It's pretty bad ;-).
I have the following setup :
- Twisted Daemon that does 5 things (very well!) - A new requirement -- to execute 2 scripts on the commandline , one after the other ( ie, blocking )
the 2 commandline scripts are just tossing some PostgreSQL commands via `postgresql db < file.txt`
i was looking at reactor.SpawnProcess -- which I know is more correct. it just seems to be a bit annoying to use , and I know the subprocess module already.
The subprocess module does not have any way to spawn a subprocess and get its output and exit status as events in the reactor; its methods all block. You can sort of re-build spawnProcess yourself from scratch, but ... then you'd just have spawnProcess with a slightly more subprocess-y API, and you'd be better off writing a utility function that wraps spawnProcess for yourself. Could you write up why you feel it's "annoying"? Maybe we can improve spawnProcess so it works better for you in the future. -glyph
On Sep 26, 2013 8:18 PM, "Glyph" <glyph@twistedmatrix.com> wrote:
On Sep 26, 2013, at 6:51 PM, Jonathan Vanasco <twisted-python@2xlp.com>
wrote:
Could you write up why you feel it's "annoying"? Maybe we can improve
spawnProcess so it works better for you in the future. I'm also interested in what you feel is annoying. Please write this (it will help improve documentation too). Also, check out http://twistedftw.org/articles.html#!/processes/getstdout Matt
been too busy to work on this, but... 1) Ok, I will avoid subprocess and stick with twisted. Thanks! 2) I will absolutely document where things got annoying to share! On Sep 27, 2013, at 12:35 AM, Matt Haggard wrote:
On Sep 26, 2013 8:18 PM, "Glyph" <glyph@twistedmatrix.com> wrote:
On Sep 26, 2013, at 6:51 PM, Jonathan Vanasco <twisted-python@2xlp.com> wrote:
Could you write up why you feel it's "annoying"? Maybe we can improve spawnProcess so it works better for you in the future.
I'm also interested in what you feel is annoying. Please write this (it will help improve documentation too).
Also, check out http://twistedftw.org/articles.html#!/processes/getstdout
Matt _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
// Jonathan Vanasco c. 646.729.6436 | 415.501.9815 e. jonathan@2xlp.com w. http://findmeon.com/user/jvanasco linkedin. http://linkedin.com/in/jonathanvanasco blog. http://destructuring.net
On 09/27/2013 02:51 AM, Jonathan Vanasco wrote:
i was looking at reactor.SpawnProcess -- which I know is more correct. it just seems to be a bit annoying to use
Are you aware of twisted.internet.utils.getProcessOutputAndValue? For simple spawn/check cases, that's pretty much always the best option IMO. If you have a case where you need to write to a child process - well, I don't see how you hope to avoid using spawnProcess and a ProcessProtocol; like others I'd be interested to hear what you dislike about the API (I have problems with a bunch of Twisted APIs, but that one I quite like ;o)
participants (9)
-
George Pauly
-
Glyph
-
glyph@divmod.com
-
Jean-Paul Calderone
-
Jonathan Vanasco
-
Matt Haggard
-
Phil Christensen
-
Phil Mayers
-
Raj kumar