[jdom-interest] best way to parse all data

Scott Purcell spurcell at vertisinc.com
Tue May 13 06:50:48 PDT 2003


Hello,

I am trying to use the jdom product to parse a simple xml document. I have been playing with this and could use some help. I got the basics started, but cannot figure out how to properly use the api.

I would like to grab each <person>block, iterate thriough it, and show the text or attributes that are available.

Here is what I have which works, and below is the simple xml.

Thanks,
Scott

package com.skp.xml.jdom;

import org.jdom.*;
import org.jdom.input.*;
import java.io.*;
import java.io.IOException;
import java.util.*;


public class showValues 
{

    public static void main(String[] args) throws JDOMException, IOException {
	if (args.length < 1) {
	    System.out.println(
		"Usage: java com.skp.xml.jdom.showValues [xml document filename]");
	    System.exit(-1);
	}


	SAXBuilder builder = new SAXBuilder();
	//	builder.setValidation(true);
	builder.setIgnoringElementContentWhitespace(true);
	Document doc = builder.build(new File(args[0]));

	Element root = doc.getRootElement();
	List theDoc = root.getChildren();
	Iterator it = theDoc.iterator();
	while (it.hasNext()) {
	    Element el = (Element)it.next();
	    parseObject(el);
	}
    }

    public static void parseObject(Element element)
    {
	String name = element.getName();
	System.out.println(name + " <element>");
	List 



    }

}
<?xml version="1.0" encoding="ISO-8859-1"?>
<family>
  <person>
    <given-name age="22">
      <name>fred</name>
      <nick-name>fred</nick-name>
    </given-name>
    <family-name>flintstone</family-name>
  </person>
  <person>
    <given-name age="30">
      <name>barney</name>
      <nick-name>barn</nick-name>
    </given-name>
    <family-name>rubble</family-name>
  </person>
</family>





More information about the jdom-interest mailing list