Note that this is not really a STAF or STAX question.  This is simply a 
question of how in Jython can I use a parse an xml file to get the values 
for the text nodes for elements "name" and "machine_name". 

Also, why are you using an xml file when instead you could simply use a 
file that contains Python code to set the values for variables name and 
machine_name?  For example, you could simply create a file, 
C:\myScriptFile.py, that contains:

name = 'QuickSanity'
machine_name = '155.35.3.79,155.63.3.89,155.62.3.91'

And then specify the name of this file as the value for the SCRIPTFILE 
option on your STAX EXECUTE request (or similarly via the STAX Monitor). 
For example:

STAF staxMachine STAX EXECUTE FILE "C:/yourStaxJob.xml'  SCRIPTFILE 
"C:/myScriptFile.py"

Then you would be able to reference Python variables name and machine_name 
from within your STAX job with no xml parsing required.

But, in case you have a good reason for the data only being available in 
an xml file, here's an example of a STAX job that parses your xml file and 
gets the values for the "name" and "machine_name" elemenst in the xml 
file.

<?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>
        [name, machine_name] = STAXResult
      </script>

      <log message="1">'Name: %s' % (name)</log>
      <log message="1">'Machine_name: %s' % (machine_name)</log>

    </sequence>
  </function>

  <!-- ******************************************************************* 
-->
  <!-- Following function is used to parse an XML file and get the text 
-->
  <!-- values for the name and machine_name elements -->
  <!-- ******************************************************************* 
-->
  <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>

      <script>
        name = None
        machine_name = None

        # Get the text value for the element with tag name "name"
 
        nodeList = document.getElementsByTagName("name")

        for i in range(nodeList.getLength()):
          node = nodeList.item(i);
 
          if node.getNodeType() == Node.ELEMENT_NODE:
            children = node.getChildNodes()

            for j in range(children.getLength()):
              thisChild = children.item(j)

              if (thisChild.getNodeType() == Node.TEXT_NODE):
                name = thisChild.getNodeValue()

        # Get the text value for the element with tag name "machine_name"

        nodeList = document.getElementsByTagName("machine_name")

        for i in range(nodeList.getLength()):
          node = nodeList.item(i)

          if node.getNodeType() == Node.ELEMENT_NODE:
            children = node.getChildNodes()

            for j in range(children.getLength()):
              thisChild = children.item(j)

              if (thisChild.getNodeType() == Node.TEXT_NODE):
                machine_name = thisChild.getNodeValue()
      </script>

      <return>[name, machine_name]</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>

--------------------------------------------------------------
Sharon Lucas
IBM Austin,   luc...@us.ibm.com
(512) 286-7313 or Tieline 363-7313




Sangram Bakshi <san...@gmail.com> 
04/06/2009 08:01 AM

To
staf-users@lists.sourceforge.net
cc

Subject
[staf-users] Parsing a XML file in a stax job.






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

------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users

Reply via email to