![]() |
|
|
|
#1 |
|
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 Prabh |
|
|
|
|
#2 |
|
Posts: n/a
|
> I'm trying to read the contents of an XML file from my Java program.
I would use JDOM. http://www.jdom.org/ -Renni Renni Lehtonen |
|
|
|
#3 |
|
Posts: n/a
|
On 4 Nov 2003 11:15:13 -0800, (Prabh) wrote:
>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; } } } -- Phil Hanna Author of JSP 2.0: The Complete Reference http://www.philhanna.com Phil Hanna |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java read xml file | java_newbie | General Help Related Topics | 1 | 06-04-2009 02:30 AM |
| xml file reading in java /deserialization in java | sowmyask | Software | 0 | 04-15-2009 08:03 AM |
| problems backing up dvds | Lawrence Traub | DVD Video | 11 | 09-27-2005 07:34 PM |
| Re: Ripping DVDs. Please answer the attached question. - Question.txt | Stan Brown | DVD Video | 19 | 02-09-2005 11:19 PM |
| Burn process failed - help! Log file posted for help troubleshooting | Michael Mason | DVD Video | 1 | 08-16-2004 09:24 PM |