Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] best way to bind java classes to xom classes?

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Arjan Huijzer <huijzer AT gmail.com>
  • To: Elliotte Harold <elharo AT metalab.unc.edu>
  • Cc: xom-interest AT lists.ibiblio.org, "John.Cowan" <jcowan AT reutershealth.com>
  • Subject: Re: [XOM-interest] best way to bind java classes to xom classes?
  • Date: Fri, 17 Jun 2005 15:53:18 +0200

I do not know if this is what you're looking for, but I have included a
simplified version of some code I wrote a while ago.
Basically I subclassed nu.xom.Element so I could add some convenience
methods.

Hope this helps,
Arjan Huijzer

=================================
book.xml
=================================
<?xml version="1.0" encoding="utf-8"?>
<book>
<chapter>
<title>Chapter 1</title>
</chapter>
<chapter>
<title>Chapter 2</title>
</chapter>
<chapter>
<title>Chapter 3</title>
</chapter>
<chapter>
<title>Chapter 4</title>
</chapter>
<chapter>
<title>Chapter 5</title>
</chapter>
</book>


=================================
Chapter.java
=================================
package org.xamples.xomtest;

import nu.xom.Element;

public class Chapter extends Element {

String title;

public Chapter(Element chapterElement) {
super(chapterElement);
}

public String getTitle() {
if (title == null) {
Element titleElement = this.getFirstChildElement("title");
title = titleElement.getValue();
}
return title;
}

}

=================================
Processor.java
=================================
package org.xamples.xomtest;

import java.io.IOException;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Elements;
import nu.xom.ParsingException;

public class Processor {

public static void main(String[] args) {

Document doc = null;

try {
Builder parser = new Builder();
doc = parser.build("book.xml");
} catch (ParsingException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}

Elements chapterElements = doc.getRootElement().getChildElements("chapter");
for (int i = 0; i < chapterElements.size(); i++) {
Chapter ch = new Chapter(chapterElements.get(i));
System.out.println( ch.getTitle() );
}

}
}



On 6/17/05, Elliotte Harold <elharo AT metalab.unc.edu> wrote:
>
> John.Cowan wrote:
>
> > Elliotte, you really need to write a tutorial explaining all this.
> >
>
> This is mentioned in the tutorial at
> http://www.xom.nu/tutorial.xhtml#d0e1422
>
> Maybe I need to expand on this. Maybe I can sell an article to
> developerWorks about it. Does anyone have a good suggestion for a
> not-too-complicated example? i.e. something that would change maybe one
> class to a subclass, but not need to rewrite everything or provide a
> huge class hierarchy?
>
> --
> Elliotte Rusty Harold elharo AT metalab.unc.edu
> XML in a Nutshell 3rd Edition Just Published!
> http://www.cafeconleche.org/books/xian3/
> http://www.amazon.com/exec/obidos/ISBN=0596007647/cafeaulaitA/ref=nosim
> _______________________________________________
> XOM-interest mailing list
> XOM-interest AT lists.ibiblio.org
> http://lists.ibiblio.org/mailman/listinfo/xom-interest
>




Archive powered by MHonArc 2.6.24.

Top of Page