[jdom-interest] Displaying a JDOM objects data content - helpneeded

Phil Weighill-Smith phil.weighill-smith at volantis.com
Thu Mar 10 12:46:24 PST 2005


I assume you just want textual content (i.e. not include elements or their attributes). If so, you could try using an XPath expression to select all text nodes only then iterate the returned list of text nodes and output the content.
 
The expression should be something like: "//text()". JDOM comes with an integration with Jaxen that can be used to perform this query on your JDOM document.
 
Phil :n)

	-----Original Message----- 
	From: sal achhala [mailto:sal at mellowmoose.org] 
	Sent: Thu 10/03/2005 13:55 
	To: Dimitrios Kolovos 
	Cc: jdom-interest at jdom.org 
	Subject: Re: [jdom-interest] Displaying a JDOM objects data content - helpneeded
	
	

	Thanks Dimitrios,
	
	Ive had some good results with the XMLOutputter class - have now managed
	to print out the xml file.
	
	Next step I'm going to try & just obtain the data contents of a simple
	xml file. I understand i'll need to use a iterator to do this ?
	
	sal
	
	Dimitrios Kolovos wrote:
	> have you tried XMLOutputter?
	>
	> Dimitrios
	>
	> ----- Original Message ----- From: "sal achhala" <sal at mellowmoose.org>
	> To: <jdom-interest at jdom.org>
	> Sent: Thursday, March 10, 2005 12:04 PM
	> Subject: [jdom-interest] Displaying a JDOM objects data content - help
	> needed
	>
	>
	>> I have a problem with using JDOM to output contents of an xml file.
	>>
	>> I’ve used the rome api to output a rss feed as a JDOM object – this
	>> works fine. However I want to output just the contents of the Jdom
	>> obect (minus the xml tags) but im having problems with the following
	>> code.
	>>
	>> Code below outputs a jdom object, using the rome api, and its copied
	>> into a jdom document
	>> Document doc = outputjdom.outputJDom(feed);
	>>
	>> Next im using the doc.toString() method ( part of jdom) to see if the
	>> document holds the xml file.
	>>
	>> System.out.println(doc.toString());
	>>
	>> However, it doesn’t display the Document contents. where am I going
	>> wrong ? and what do I need to do to start outputing the contents ? I
	>> think the problem lies with the way im assigning the ouputted jdom
	>> object to a jdom document here:
	>>
	>> Document doc = outputjdom.outputJDom(feed);
	>>
	>> any ideas would be appreciated.
	>>
	>> console output when the code is run:
	>>
	>> init:
	>> deps-jar:
	>> compile:
	>> run:
	>> [Document: No DOCTYPE declaration, Root is [Element: <rss/>]]
	>> content size is 1
	>> BUILD SUCCESSFUL (total time: 2 seconds)
	>>
	>>
	>>
	>>
	>>
	>>
	>>
	>> /*
	>> * Created on Feb 23, 2005
	>> */
	>>
	>> import com.sun.syndication.feed.synd.SyndFeed;
	>> import com.sun.syndication.io.SyndFeedInput;
	>> import com.sun.syndication.io.XmlReader;
	>> import com.sun.syndication.io.SyndFeedOutput;
	>> import java.io.File;
	>> import java.io.StringReader;
	>> import java.net.URL;
	>>
	>> import org.jdom.output.XMLOutputter;
	>> import org.jdom.*;
	>> import org.jdom.Document;
	>> import org.jdom.input.SAXBuilder;
	>>
	>> import java.io.*;
	>> import java.util.*;
	>>
	>> /**
	>>  * It Reads and prints any RSS/Atom feed type.
	>> *
	>>  */
	>> public class FeedReader {
	>>
	>>     public static void main(String[] args) {
	>>
	>>         try {
	>>             URL feedUrl = new URL(args[0]);
	>>
	>>     //to read in & parse the rss/atom feed frm the internet
	>>     //based on the ROME API
	>>             SyndFeedInput input = new SyndFeedInput();
	>> //syndfeed is a bean interface which conatins the entire rss feed for
	>> //the given URL
	>>
	>>         SyndFeed feed = input.build(new XmlReader(feedUrl));
	>>
	>>
	>>
	>> //prepare output of the retrieved feed as a Jdom object, xml string or
	>> //write to a file
	>>         SyndFeedOutput outputjdom = new SyndFeedOutput();
	>>
	>> // will output feed as jdom
	>>             Document doc = outputjdom.outputJDom(feed);
	>>
	>>
	>> //doc.toString() is part of jdom – should output the document to
	>> console System.out.println(doc.toString());
	>>
	>>
	>>          Element root = doc.getRootElement();
	>>     System.out.println("content size is "+root.getContentSize());
	>>         }//end try
	>>         catch (Exception ex) {
	>>             ex.printStackTrace();
	>>             System.out.println("ERROR: "+ex.getMessage());
	>>         }//catch
	>>
	>>     }//end main
	>>
	>> }//end class
	>>
	>> _______________________________________________
	>> To control your jdom-interest membership:
	>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
	>>
	>
	>
	>
	>
	
	




More information about the jdom-interest mailing list