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
}