Read XML file from Java

P

Prabh

Hello all,
I'm trying to read the contents of an XML file from my Java program.

Sample:
====================================================

<tag name="First" >
<Line> One </Line>
<Line> Two </Line>
</tag>

<tag name ="Second" >
<Line> Three </Line>
<Line> Four </Line>
</tag>
====================================================

I want to extract info from the tags and after some parsing, display
them as:

From first tag: This is processed info. on lines One, Two
From second tag: This is processed info. on lines Three, Four

As you can see, I dont want anything fancy, just some ability to
specify a file and some specific tags that
I'm interested in and be able to extract info b/w those tags.

1) Is this possible with SAX, I dont see examples dealing with
"specify tags and get info."
2) I maynot be in a position to install the sax jars, are there any
easy ways to do this without SAX?

Thanks,
Prab
 
P

Phil Hanna

Hello all,
I'm trying to read the contents of an XML file from my Java program.

Sample:
====================================================

<tag name="First" >
<Line> One </Line>
<Line> Two </Line>
</tag>

<tag name ="Second" >
<Line> Three </Line>
<Line> Four </Line>
</tag>
====================================================

I want to extract info from the tags and after some parsing, display
them as:

From first tag: This is processed info. on lines One, Two
From second tag: This is processed info. on lines Three, Four

As you can see, I dont want anything fancy, just some ability to
specify a file and some specific tags that
I'm interested in and be able to extract info b/w those tags.

1) Is this possible with SAX, I dont see examples dealing with
"specify tags and get info."
2) I maynot be in a position to install the sax jars, are there any
easy ways to do this without SAX?

Thanks,
Prab

There are two standard approaches to parsing XML in a Java program:

1. DOM - creates a tree you can navigate at random
with various methods

2. SAX - creates events for the start of an element, the end of
an element, for text, etc., and calls callback methods
that you write to handle them.

(I wouldn't bother with JDOM - it doesn't scale, it's not
interface-based so there's only one implementation, it's not supported
by the original author)

I've attached complete examples for DOM and SAX that do what you
describe in your note. They're a little more complicated than
necessary, because I was trying to reproduce your output exactly.
You'll see the logical places to make alterations.

Here is your data file:

==========================
data.xml
==========================

<?xml version="1.0"?>
<data>
<tag name="First" >
<Line> One </Line>
<Line> Two </Line>
</tag>
<tag name ="Second" >
<Line> Three </Line>
<Line> Four </Line>
</tag>
<tag name ="Third" >
<Line> Five </Line>
<Line> Six </Line>
</tag>
</data>

==========================
DOMExample.java
==========================

import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class DOMExample
{
public static void main(String[] args)
{
File file = new File("data.xml");
try {

// Parse the file

DocumentBuilder builder = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(file);

// Find the tags of interest

NodeList nodes = doc.getElementsByTagName("tag");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);

// Skip all but those named "First" or "Second"

String name = element.getAttribute("name");
if (!name.equals("First") && !name.equals("Second"))
continue;

String message = "From " + name + " tag: "
+ "This is processed info. on lines";

// Process the lines

NodeList lines = element.getElementsByTagName("Line");
for (int j = 0; j < lines.getLength(); j++) {
Element line = (Element) lines.item(j);
if (j > 0)
message += ",";
message += " ";

// Collect the text from the <Line> element

StringBuffer sb = new StringBuffer();
for (Node child = line.getFirstChild();
child != null;
child = child.getNextSibling())
{
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
sb.append(cd.getData());
}
}
String text = sb.toString().trim();
message += text;
}

// Print the result

System.out.println(message);
}
}
catch (Exception e) {
// Do something with the exception
}
}
}

==========================
SAXExample.java
==========================
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class SAXExample extends DefaultHandler
{
public static void main(String[] args)
{
File file = new File("data.xml");
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
DefaultHandler handler = new SAXExample();
parser.parse(file, handler);
}
catch (Exception e) {
// Do something with the exception
}
}

private boolean insideTag, insideLine;
private String message;
private StringBuffer lineBuffer;
private int lineCounter;

public void startElement(String ns, String ln, String qname,
Attributes atts)
throws SAXException
{
if (qname.equals("tag")) {
String name = atts.getValue("name");
if (name.equals("First") || name.equals("Second")) {
insideTag = true;
message = "From " + name + " tag:"
+ " This is processed info. on lines ";
lineCounter = 0;
}
}
else
if (insideTag && qname.equals("Line")) {
insideLine = true;
lineBuffer = new StringBuffer();
lineCounter++;
}
}

public void characters(char[] ch, int offset, int length)
throws SAXException
{
if (insideLine) {
lineBuffer.append(new String(ch, offset, length));
}
}

public void endElement(String ns, String ln, String qname)
throws SAXException
{
if (qname.equals("tag")) {
if (insideTag) {
insideTag = false;
System.out.println(message);
message = null;
lineCounter = 0;
}
}
else
if (insideTag && qname.equals("Line")) {
String text = lineBuffer.toString().trim();
if (lineCounter > 1) {
message += ", ";
}
message += text;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top