From: Divya P. <Di...@am...> - 2006-12-19 09:36:45
|
Hi All, I m facing the problem while parsing xml file .. My code is :- import sys from org.apache.xerces.parsers import DOMParser as dp import javax.xml.parsers infilename = open("mos.xml","r") print infilename def test(infilename): """Parse XML document and show attributes and names. """ print infilename parser = dp() print parser gh = parser.parse(infilename) print gh doc = parser.getDocument() node = doc.getFirstChild() print "Attributes:" show_attrs(node) print "Names:" show_names(node) def show_attrs(node): """Show the attributes and their values. """ node = node.getFirstChild() while node: if node.getNodeType() == node.ELEMENT_NODE: print " %s:" % (node.getTagName(), ) attrs = node.getAttributes() count = attrs.getLength() for idx in range(count): attr = attrs.item(idx) print " %s: %s" % ( attr.getNodeName(), attr.getNodeValue(), ) node = node.getNextSibling() def show_names(node): """Show the value of the name element for each person element. """ node = node.getFirstChild() while node: if (node.getNodeType() == node.ELEMENT_NODE and node.getTagName() == "person"): show_person_name(node) node = node.getNextSibling() def show_person_name(node): node = node.getFirstChild() while node: if (node.getNodeType() == node.ELEMENT_NODE and node.getTagName() == "name"): show_text("name: ", node) node = node.getNextSibling() def show_text(msg, node): """Show a message and the value of a text node. """ node = node.getFirstChild() while node: if node.getNodeType() == node.TEXT_NODE: print "%s %s" % (msg, node.getNodeValue(), ) node = node.getNextSibling() ''' def usage(): print "Usage: jython test_xerces.py <infilename>" sys.exit(-1) ''' test(infilename) Error :--- Type Error : parse() :1sr arg cant be coerced to org.xml.sax.InputSource ans String |