From abhishek.kane at gmail.com Wed Sep 16 17:33:48 2009 From: abhishek.kane at gmail.com (Abhishek Kane) Date: Wed, 16 Sep 2009 21:03:48 +0530 Subject: [XML-SIG] Modify node attributes in XML file while parsing Message-ID: Hi Guys, I got a bit idea about parsing XML file from online search. I need to modify node attributes in an XML file while I read it. I could read node attributes; but when I modify them using setAttribute, the local copy of that node gets changed. Is there any way to directly modify the node attributes in XML file or shall I just create another XML file where I will create nodes as I go through the nodes in old XML file? Thanks, Abhishek -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Wed Sep 16 19:20:06 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 16 Sep 2009 19:20:06 +0200 Subject: [XML-SIG] Modify node attributes in XML file while parsing In-Reply-To: References: Message-ID: <4AB11E46.9090304@behnel.de> Abhishek Kane wrote: > I got a bit idea about parsing XML file from online search. I need to modify > node attributes in an XML file while I read it. I could read node > attributes; but when I modify them using setAttribute, the local copy of > that node gets changed. > Is there any way to directly modify the node attributes in XML file or shall > I just create another XML file where I will create nodes as I go through the > nodes in old XML file? Could you explain a bit more about your intentions? What is the code doing? I understand that the XML data is in a file that you parse, but where is the data going afterwards? Do you use it in your program? Do you save it back into a file? Stefan From joshua.r.english at gmail.com Thu Sep 17 02:24:55 2009 From: joshua.r.english at gmail.com (Josh English) Date: Wed, 16 Sep 2009 17:24:55 -0700 Subject: [XML-SIG] Modify node attributes in XML file while parsing In-Reply-To: References: Message-ID: Everything works, but you need to write this over data.xml. I don't remember how to do this in minidom. I think writing to an output file is the only way to do this: o = open('data.xml') o.write(xmldoc.toxml()) o.close() On Wed, Sep 16, 2009 at 9:58 AM, Abhishek Kane wrote: > > > > > ??? > > > ??? > > > > > > ?is how the XML file looks like. I want to modify the value of instance > based on the service name. > > So if i search for abc service & modify instance value like follows : > > > import sys, logging, traceback > from xml.dom import minidom > > data_file = "/tmp/data.xml" > > def main(): > ??? try: > ??? ??? xmldoc = minidom.parse(data_file) > ??? except IOError: > ??? ??? print "err" > ??? ??? sys.exit(1) > > ??? start = xmldoc.firstChild > > ??? for element in xmldoc.getElementsByTagName("service"): > ??? ??? service = element.getAttribute("name") > ??? ??? print service > ??? ??? if service == "abc": > ??? ??? ??? for instance in element.childNodes: > ??? ??? ??? ??? if instance.nodeType == 1: > ??? ??? ??? ??? ??? print instance.getAttribute("value") > ??? ??? ??? ??? ??? instance.setAttribute("value", "true") > ??? ??? ??? ??? ??? print instance.getAttribute("value") > > if __name__ == "__main__": > ??? logging.basicConfig(filename=log_file,level=logging.DEBUG) > ??? main() > > The first print gives false & later one gives true. But the data.xml remains > unchanged. How can I modify the node in data.xml? > > Thanks, > Abhishek > > On Wed, Sep 16, 2009 at 9:47 PM, Josh English > wrote: >> >> If you are just searching for XML nodes, you are searching. If you are >> interpreting them into something new, you are parsing. >> >> If you want to change an attribute, parse the XML file and make changes >> there. >> >> Either that, or read the node attribute that you are given, make the >> changes you need, but don't bother with writing a new XML file. >> >> Perhaps a sample of what you are trying to do would help up see the >> solution. >> >> On 9/16/09, Abhishek Kane wrote: >> > Hi Guys, >> > >> > I got a bit idea about parsing XML file from online search. I need to >> > modify >> > node attributes in an XML file while I read it. I could read node >> > attributes; but when I modify them using setAttribute, the local copy of >> > that node gets changed. >> > Is there any way to directly modify the node attributes in XML file or >> > shall >> > I just create another XML file where I will create nodes as I go through >> > the >> > nodes in old XML file? >> > >> > Thanks, >> > Abhishek >> > >> >> >> -- >> Josh English >> Joshua.R.English at gmail.com >> http://joshenglish.livejournal.com > > > > -- > No defeat is final; until u stop fighting. > - AGK > > -- Josh English Joshua.R.English at gmail.com http://joshenglish.livejournal.com From abhishek.kane at gmail.com Thu Sep 17 09:47:40 2009 From: abhishek.kane at gmail.com (Abhishek Kane) Date: Thu, 17 Sep 2009 13:17:40 +0530 Subject: [XML-SIG] Modify node attributes in XML file while parsing In-Reply-To: References: Message-ID: * * is how the XML file looks like. I want to modify the *value *of *instance *based on the *service name*. So if i search for *abc* service & modify instance value like follows : *import sys, logging, traceback from xml.dom import minidom data_file = "/tmp/data.xml" def main(): try: xmldoc = minidom.parse(data_file) except IOError: print "err" sys.exit(1) start = xmldoc.firstChild for element in xmldoc.getElementsByTagName("service"): service = element.getAttribute("name") print service if service == "abc": for instance in element.childNodes: if instance.nodeType == 1: print instance.getAttribute("value") instance.setAttribute("value", "true") print instance.getAttribute("value") if __name__ == "__main__": logging.basicConfig(filename=log_file,level=logging.DEBUG) main() * The first print gives *false* & later one gives *true*. But the *data.xml*remains unchanged. How can I modify the node in data.xml? Thanks, Abhishek On Wed, Sep 16, 2009 at 9:47 PM, Josh English wrote: > If you are just searching for XML nodes, you are searching. If you are > interpreting them into something new, you are parsing. > > If you want to change an attribute, parse the XML file and make changes > there. > > Either that, or read the node attribute that you are given, make the > changes you need, but don't bother with writing a new XML file. > > Perhaps a sample of what you are trying to do would help up see the > solution. > > On 9/16/09, Abhishek Kane wrote: > > Hi Guys, > > > > I got a bit idea about parsing XML file from online search. I need to > modify > > node attributes in an XML file while I read it. I could read node > > attributes; but when I modify them using setAttribute, the local copy of > > that node gets changed. > > Is there any way to directly modify the node attributes in XML file or > shall > > I just create another XML file where I will create nodes as I go through > the > > nodes in old XML file? > > > > Thanks, > > Abhishek > > > > > -- > Josh English > Joshua.R.English at gmail.com > http://joshenglish.livejournal.com > -- No defeat is final; until u stop fighting. - AGK -------------- next part -------------- An HTML attachment was scrubbed... URL: From bigotp at acm.org Sat Sep 19 19:28:15 2009 From: bigotp at acm.org (Peter A. Bigot) Date: Sat, 19 Sep 2009 10:28:15 -0700 Subject: [XML-SIG] ANN: Updated Python/XML Binding release available Message-ID: <4AB514AF.7090906@acm.org> Several months ago I announced the initial public release of PyXB, a pure Python package that generates Python bindings from XML schema. PyXB is now feature-complete at version 0.7.3, with an expected posting of the stable version 1.0.0 by the end of September. The documentation is at http://pyxb.sourceforge.net, and the release can be downloaded from links on that page. PyXB supports almost all features of XML schema, including: * Class constants corresponding to string enumeration constraints * Simple and complex type definitions * List and union datatypes * Constraints on (simple) datatypes (e.g., minInclusive, length) * Model groups and attribute groups * Complex content models (all, sequence, choice); minOccurs and maxOccurs * Abstract types, xsi:type, substitution groups * Nillable elements with xsi:nil * Namespace qualified attributes and elements * Import and include directives * Documentation annotations present in the schema are converted to Python docstrings in the generated bindings. * Pattern constraints (as long as the simple type derives from something that is represented as a Python string) The full package includes bindings for almost two dozen WS-*--related schema including SOAP and WSDL, and the complete set of schema for OpenGIS. Its limitations are that it has only crude support for bindings that do not validate, does not support the "redefine" directive, and is not always as informative regarding why a particular document is invalid as one might wish. A users mailing list is available at http://sourceforge.net/mailarchive/forum.php?forum_name=pyxb-users. I would appreciate any feedback. Peter