Hi , I am trying to parse a xml file and read values for it. For this i am trying to use the DocumentBuilderFactory. The xml i am trying to read is :
<?xml version='1.0' encoding='utf-8'?> <Mydata> <name>QuickSanity</name> <machine_name>155.35.3.79,155.63.3.89,155.62.3.91</machine_name> </Mydata> I want to read the data in tag's <name> , <machine_name> (the array of ip address ). I want to read these values and store them in variables so that i can use them in my stax job later on. I am parsing them this way: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE stax SYSTEM "stax.dtd"> <stax> <defaultcall function="Main"/> <function name="Main"> <sequence> <!-- Assign the file name of a STAX xml document you want to parse --> <call function="'parseXML'">'c:/MyXml.xml'</call> <script> document = STAXResult <I am not sure sure how to use the document builder API to get the xml files values here> msg = '%s\nFound %s element ' % (msg, root.getElementsByTagName("name")) for i in range(children.getLength()): thisChild = children.item(i); if (thisChild.getNodeType() == Node.ELEMENT_NODE and thisChild.getNodeName() == 'defaultcall'): msg = '%s\nFound defaultcall element' % (msg) elif thisChild.getNodeType() == Node.COMMENT_NODE: # Do nothing continue elif thisChild.getNodeType() == Node.ELEMENT_NODE: msg = '%s\nFound %s element ' % (msg, thisChild.getNodeName()) </script> <message>'Some parsed data: %s' % (msg)</message> <log>'Some parsed data: %s' % (msg)</log> </sequence> </function> <!-- ******************************************************************* --> <!-- Following function is used to parse an XML file and return the DOM --> <!-- document object --> <!-- ******************************************************************* --> <function name="parseXML" scope="local"> <function-list-args> <function-required-arg name="xmlFileName"> Name of file containing XML to be parsed </function-required-arg> </function-list-args> <sequence> <!-- Parse the XML --> <script> factory = DocumentBuilderFactory.newInstance(); factory.setValidating(1) factory.setIgnoringElementContentWhitespace(0) builder = factory.newDocumentBuilder() document = builder.parse(xmlFileName) </script> <return>document</return> </sequence> </function> <script> # These imports only need to be done once per job, no matter # how many xml documents are parsed from java.io import File from java.io import StringReader from org.xml.sax import InputSource from org.xml.sax import SAXParseException from org.xml.sax.helpers import DefaultHandler from javax.xml.parsers import DocumentBuilderFactory from javax.xml.parsers import DocumentBuilder from org.w3c.dom import Document from org.w3c.dom import Element from org.w3c.dom import Node from org.w3c.dom import NodeList </script> </stax>
------------------------------------------------------------------------------
_______________________________________________ staf-users mailing list staf-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/staf-users