Results 1 to 2 of 2
- 08-01-2009, 01:12 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
Parsing XML using Document Builder Factory
Hi ,
I have to parse the following XML file using DocumentBuilder Factory:
This parsing is done in STAF STAX job.
I am not able to do so properly.Kindly suggest.
<?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>
In the above XML file i am able to read values off all tags except:
"machine_name" and "vmware"
For machine_name tag , i have to read it and store all the values as a list say
mach_name which should have the contents [u80sp1_L005,u80sp1_L006,u80sp1_L007]
also all the vmware attributes should be read into
vmware_attr which should have the content as :[155.35.3.55,155.35.3.56]
also all the vmware values should be read into
vmware_value which should have the content as :
[144.35.3.90,144.35.3.91]
The code that i use to parse
vmware_attr = None
mach_name = None
vmware_value = None
# 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):
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):
mach_name = thisChild.getNodeValue()
(i am able to read here only one value)
vmware_attr = None
mach_name = None
vmware_value = None
# 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):
name = thisChild.getNodeValue()
# Get the text value for the element with tag name "vmware"
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):
vmware_value = thisChild.getNodeValue()
(i am able to read here only one value the last one)
how can i read all the vmware values?
Also i am not sure as to how i can read the vmware attributes "vmware_attr"
Kindly suggest.
Regards
Sangram
- 08-01-2009, 01:15 PM #2
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
Also the imports i am doing to support the coding are :
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
Also the language use here is jython.
Regards
Sangram
Similar Threads
-
Formula Builder
By rbs100 in forum Advanced JavaReplies: 1Last Post: 07-03-2009, 06:57 PM -
Netbeans Mobility Pack 6.0 and the Game Builder tool
By gissah in forum New To JavaReplies: 0Last Post: 03-26-2009, 04:07 PM -
Eclipse GUI Builder
By elebal in forum EclipseReplies: 1Last Post: 10-23-2008, 11:38 AM -
Creating Document object for XML parsing
By Java Tip in forum Java TipReplies: 0Last Post: 11-19-2007, 04:12 PM -
Help with NetBeans 5 GUI builder
By Daniel in forum NetBeansReplies: 2Last Post: 07-04-2007, 07:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks