• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to get information from a XML file?

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Does anyone know how to get the information in an XML file in Java?
For instance in this easy example, to retrieve the text in the first section here.
<?xml version="1.0"?>
<ARTICLE>
<TITLE>A Sample Article</TITLE>
<SECT>The First Major Section
<PARA>This section will introduce a subsection.</PARA>
<SECT>The Subsection Heading
<PARA>This is the text of the subsection.
</PARA>
</SECT>
</SECT>
</ARTICLE>
For those that are interested in XML converting, Here is a example of a code that takes a xml and xsl file and generates a html:
/**
* @author speaag
* For taking a xml and xsl file to generate .html
*/
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
// For write operation
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
public class StylizerVirker
{
// Global value so it can be ref'd by the tree-adapter
static Document document;
public static void main (String argv [])
{
/* if (argv.length != 2) {
System.err.println ("Usage: java Stylizer filename");
System.exit (1);
}*/

DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
//factory.setNamespaceAware(true);
//factory.setValidating(true);

try {
//File stylesheet = new File(argv[0]);
//File datafile = new File(argv[1]);
File stylesheet = new File("D:\\test\\article1a.xsl");
File datafile = new File("D:\\test\\article.xml");

DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(datafile);
// Use a Transformer for output
TransformerFactory tFactory =
TransformerFactory.newInstance();
StreamSource stylesource = new StreamSource(stylesheet);
tFactory.newTransformer(stylesource);
DOMSource source = new DOMSource(document);
//StreamResult result = new StreamResult(System.out);
StreamResult result = new StreamResult("D:\\test\\out.html");
Transformer transformer = tFactory.newTransformer(stylesource);
transformer.transform(source, result);
}
catch (TransformerException te) {
// Error generated by the parser
System.out.println ("\n** Transformation error");
System.out.println(" " + te.getMessage() );
// Use the contained exception, if any
Throwable x = te;
if (te.getException() != null)
x = te.getException();
x.printStackTrace();

}

catch (SAXException sxe) {
// Error generated by this application
// (or a parser-initialization error)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}
} // main
}
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know the open source project Castor? http://www.castor.org/
To repeat the developers of castor :
Castor is an open source data binding framework for Java[tm]. It's basically the shortest path between Java objects, XML documents and SQL tables. Castor provides Java to XML binding, Java to SQL persistence, and then some more.
I've used it several times and found it easy and elegant to use.
You don't have to write java-files anymore to parse the xml-file.
You simply generate it. In your situation a method call would be enough to, for example, retrieve the TITLE. When you want to know more, just ask.
 
age spets
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help, looked at it, but it did not help me much. Found instead this beauty of a page:
http://www.fairfieldjug.com/assets/ProcessingXMLwithJava.ppt
This helped me find this code:
// Get the root element of the document.Element
Element rootElement = document.getDocumentElement();
// Get the first child
Node node = rootElement.getFirstChild();
// Get all nodes which have the tag name foo.
NodeList list = document.getElementsByTagName("address");
Developed that a bit further and I got:
Node node2=list.item(0).getFirstChild().getNodeValue();
node2 gives me then the value of the tag adress.
 
Arnold Reuser
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe castor was, in your situation, indeed a little bit too much.
Nice to hear that you've found a more suitable solution.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic