dcsimg
XML
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anitha_Pavaman
Posted On:   Monday, October 1, 2001 05:13 AM

Sonnet 130 I'm getting a sealing Exception when I run a Java program parsing an xml file. Java Program public class domOne { public void parseAndPrint(String uri) { Document doc = null; System.out.println("URI Value..."+uri); try { DOMParser parser = new DOMParser(); System.out.println("Parser initialised"); parser.parse(uri); doc = parser.getDocument(); } catch (Exception e) { System.err.println("Sorry, an error occurred: "+e); //System.out.println(e.printStackTrace()); } // We've parsed the document now, so let's print it.    More>>
Sonnet 130

I'm getting a sealing Exception when I run a Java program parsing an xml file.


Java Program


public class domOne
{
public void parseAndPrint(String uri)
{
Document doc = null;
System.out.println("URI Value..."+uri);
try
{
DOMParser parser = new DOMParser();
System.out.println("Parser initialised");

parser.parse(uri);
doc = parser.getDocument();
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: "+e);
//System.out.println(e.printStackTrace());
}


// We've parsed the document now, so let's print it.

if (doc != null)
printDOMTree(doc);
}
/** Prints the specified node, then prints all of its children. */
public void printDOMTree(Node node)
{
int type = node.getNodeType();
switch (type)
{
// print the document element
case Node.DOCUMENT_NODE:
{
System.out.println(" <?xml version="1.0" ?>");
printDOMTree(((Document)node).getDocumentElement());
break;
}
// print element with attributes
case Node.ELEMENT_NODE:
{
System.out.print(" <");
System.out.print(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++)
{
Node attr = attrs.item(i);
System.out.print(""+ attr.getNodeName() +
"="" + attr.getNodeValue() +
""");
}
System.out.println(">");
NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++)
printDOMTree(children.item(i));
}
break;
}
// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
System.out.print("&");
System.out.print(node.getNodeName());
System.out.print(";");
break;
}
// print cdata sections
case Node.CDATA_SECTION_NODE:
{
System.out.print(" <![CDATA[");
System.out.print(node.getNodeValue());
System.out.print("]]>");
break;
}
// print text

case Node.TEXT_NODE:
{
System.out.print(node.getNodeValue());
break;
}
// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE:
{
System.out.print(" <?");
System.out.print(node.getNodeName());
String data = node.getNodeValue();
{
System.out.print("");
System.out.print(data);
}
System.out.print("?>");
break;
}
}
if (type == Node.ELEMENT_NODE)
{
System.out.println();
System.out.print(" </");
System.out.print(node.getNodeName());
System.out.print('>');
}
}
/** Main program entry point. */
public static void main(String argv[])
{
if (argv.length == 0)
{
System.out.println("Usage: java domOne uri");
System.out.println(" where uri is the URI of the XML document you want to print.");
System.out.println(" Sample: java domOne sonnet.xml");
System.exit(1);
}
domOne d1 = new domOne();
d1.parseAndPrint(argv[0]);
}
}

XML File


<?xml version="1.0"?>



Shakespeare
William
British
1564
1616



My mistress' eyes are nothing like the sun,
Coral is far more red than her lips red.
If snow be white, why then her breasts are dun,
If hairs be wires, black wires grow on her head.
I have seen roses damasked, red and white,
But no such roses see I in her cheeks.
And in some perfumes is there more delight
Than in the breath that from my mistress reeks.
I love to hear her speak, yet well I know
That music hath a far more pleasing sound.
I grant I never saw a goddess go,
My mistress when she walks, treads on the ground.
And yet, by Heaven, I think my love as rare
As any she belied with false compare.

   <<Less
About | Sitemap | Contact