Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

Re: [jdom-interest] Parsing XML with JDOM

Per Norrman

2006-08-09

Replies:

Or,


List l = XPath.selectNodes(document, "/record/items/subitem");
for (Iterator i = l.iterator(); i.hasNext(); ) {
  Element e = (Element) it2.next();
  System.out.println(e.getAttributeValue("period")+" for " +
e.getParent().getAttributeValue("name")+" is "+e.getText());
}

/pmn


Edelson, Justin skrev:
> There's a bunch of ways to do this, one being:
>
> Element oRoot = oDoc.getRootElement();
> List items = oRoot.getChildren("item");
> for (Iterator it = items.iterator(); it.hasNext(); ) {
>  Element item = (Element) it.next();
>  List subItems = item.getChildren("subitem");
>  for (Iterator it2 = subItems.iterator(); it.hasNext(); ) {
>   Element subItem = (Element) it2.next();
>   System.out.println(subItem.getAttributeValue("period")+" for
> +"item.getAttributeValue("name")+" is "+subItem.getText());
>  }
> }
>
> -----Original Message-----
> From: jdom-interest-bounces@(protected)
> [mailto:jdom-interest-bounces@(protected)
> Sent: Wednesday, August 09, 2006 2:26 PM
> To: jdom-interest@(protected)
> Subject: [jdom-interest] Parsing XML with JDOM
>
>
> I'm trying to get the children of children out of this XML file and I'm
> having a heck of a time. I've read every website you could possibly
> think
> of, and cannot figure out how to pull out Children out of Children. I
> have
> attached my Java file, which can pull out the root and child nodes, but
> not
> children of the children. Any help is greatly appreciated. And yes, I
> know
> I'm missing brackets at the end. How do I get the subitem and it's
> attributes????Iterator???
>
> I have an XML file like this:
> <?xml version="1.0" encoding="UTF-8" ?>
> <record>
>  <item type="stock" name="XM">
>     <subitem period="fiveyear">0.20</subitem>
>     <subitem period="tenyear">0.40</subitem>
>   </item>
>   <item type="stock" name="Sirius">
>     <subitem period="fiveyear">0.20</subitem>
>     <subitem period="tenyear">0.40</subitem>
>   </item>
> </record> http://www.nabble.com/user-files/237/satradio.java
> satradio.java

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.