• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML Parsing problem

 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again got into trouble. This time i am not able to find out one sequence while parsing a XML document. Here is the problem

My java parser code is:



and the XML file which i am passing as argument while running the code is


Now my problem is when i execute the program i am getting this sequence as output


Whenever it finds a TEXTNODE i am printing that node value.But,while parsing i noticed that the code is not able to identify closing tags(i.e.,)Once a <view> is parsed i can able to see view in my console but the same is not happening in the case of </view>.

In what way i have to change the code to get that recognized?
 
K.Suresh Kumar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

The following code will useful to you......


//Example XML document
/*
An XML Document Containing a Simple Contact List

<?xml version="1.0" standalone="yes"?>

<folks>
<person>
<phone>306 555-9999</phone>
<email>joe@webserver.net</email>
<name>Yin, Joe</name>
</person>
<person>
<phone>704 555-0000</phone>
<name>Pet, Rob</name>
<email>rob@server.com</email>
</person>
</folks>

*/

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class DOMDump {
static public void main(String[] arg) {
String filename = null;
boolean validate = false;

if (arg.length == 1) {
filename = arg[0];
} else if (arg.length == 2) {
if (!arg[0].equals("-v"))
usage();
validate = true;
filename = arg[1];
} else {
usage();
}

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);

// Parse the input to produce a parse tree with its root
// in the form of a Document object
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
builder.setErrorHandler(new MyErrorHandler());
InputSource is = new InputSource(filename);
doc = builder.parse(is);
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}

// Use a TreeDumper to list the tree
TreeDumper td = new TreeDumper();
td.dump(doc);
}

private static void usage() {
System.err.println("Usage: DOMCheck [-v] <filename>");
System.exit(1);
}
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.COMMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}

NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");

}
}
class MyErrorHandler implements ErrorHandler {
public void warning(SAXParseException e) throws SAXException {
show("Warning", e);
throw (e);
}

public void error(SAXParseException e) throws SAXException {
show("Error", e);
throw (e);
}

public void fatalError(SAXParseException e) throws SAXException {
show("Fatal Error", e);
throw (e);
}

private void show(String type, SAXParseException e) {
System.out.println(type + ": " + e.getMessage());
System.out.println("Line " + e.getLineNumber() + " Column "
+ e.getColumnNumber());
System.out.println("System ID: " + e.getSystemId());
}
}


Regards,
Suresh Kumar.K
 
Paul Clapham
Sheriff
Posts: 21154
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's very strange. You have code that prints out the value of text nodes, but you claim it prints out element names. It looks like you aren't running the code you posted.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Suresh Kumar for your code.
But, i want to figure out in what way i have to change the code which i wrote to solve my problem.

Regarding Paul's Reply:
I just want to print the node name's (i.e.,) Whenever i parse a node <view> then i must get a view printed in console.But,the same thing is not happening when i parse </view> node.Then how can i recognize the closing tags in XML.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this one is off to the intermediate forum
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are manipulating a DOM - DOM models automatically take care of closing tag logic when a NODE is created. Thus, when you have finished with the contents of a NODE there is nothing left to do.

It is the SAX and StAX parsing models that give you specific events for closing tags.

Bill
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
You are manipulating a DOM - DOM models automatically take care of closing tag logic when a NODE is created. Thus, when you have finished with the contents of a NODE there is nothing left to do.

It is the SAX and StAX parsing models that give you specific events for closing tags.

Bill


Ya thanks William,

I figured it out after searching a little and finished implementing SAX Parser for my requirement and its works great now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic