I'm not sure exactly what's wrong with your job, but we provide the 
following STAX job:  http://staf.sourceforge.net/current/getLatest.xml 
which you may want to use as an example, since it does some similar 
processing (for example it parses for multiple <file> elements).

Actually the DocumentBuilderFactory and related classes are Java classes 
(not Jython), so searching on the web for the following should provide 
some useful links.
 
javax.xml.parsers.DocumentBuilderFactory
javax.xml.parsers.DocumentBuilder
org.w3c.dom.Document
org.w3c.dom.Element
org.w3c.dom.Node
org.w3c.dom.NodeList

Thanks, 
David 

David Bender 
STAF/STAX Development 
IBM Software Group, WPLC 
 
11501 Burnet Rd. 
Bldg. 903-5B002 
Austin, TX 78758-3400 
 
Phone (T/L): 1-512-286-5315 (363-5315) 
ITN: 23635315 
Email: bda...@us.ibm.com 
 





Sangram Bakshi <san...@gmail.com> 
07/30/2009 05:17 AM

To
staf-users@lists.sourceforge.net
cc

Subject
[staf-users] Parsing a xml file using document builder factory.






Hi ,
 
This is not really related to STAF and STAX , but would be a time saver .I 
am quite new to jython xml parsing using document builder.
I am trying to read a xml file of the following format in the staf and 
stax job xml file:
 
<?xml version="1.0" encoding="utf-8"?>
<operating_system>
<unix_80sp1>
<tests type="quick_sanity_test">
<prerequisitescript>preparequicksanityscript</prerequisitescript>
<acbuildpath>acbuildpath</acbuildpath>
<testsuitscript>test quick sanity script</testsuitscript>
<testdir>quick sanity dir</testdir>
</tests>
<machine_name>u80sp1_L004</machine_name>
<machine_name>u80sp1_L005</machine_name>
<machine_name>xyz.pxy.dxe.cde</machine_name>
<vmware id="155.35.3.55">144.35.3.90</vmware>
<vmware id="155.35.3.56">144.35.3.91</vmware>
</unix_80sp1>
</operating_system>
 
Here machine_name tag can be n number. I need to read all the machine_name 
tags in  an array , so that i can use them later.
Also i need to read all the vmware  tags in an array (both value and 
attributes) so that i can use them later. The number of vmware tags can 
again be n numbers.
 
I am trying the following code :
 
  <!-- ******************************************************************* 
-->
  <!-- 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>
  <script> 
        
        vmware  = None
        machine_name = None
 
       # Get the text value for the element with tag name "vmware" 
        nodeList = document.getElementsByTagName("vmware") 
        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()
    
  # Get the text value for the element with tag name "machine_name" 
        nodeList = document.getElementsByTagName("vmware") 
        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>[machine_name,vmware]</return> 
   </sequence>
  </function> 
 
 
I am able to read only one value for machine_name (u80sp1_L005) and also 
one value for vmware 
i.e 144.35.3.91.
 
Also a good link to understand parsing in jython using doumentbilder 
factory would be good help.
 
Regards
Sangram
 
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 
30-Day 
trial. Simplify your report design, integration and deployment - and focus 
on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users

Reply via email to