Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 java.io.File;
- import java.io.IOException;
- import org.w3c.dom.Document;
- import org.w3c.dom.DOMException;
- import org.w3c.dom.NodeList;
- import org.w3c.dom.Node;
- import org.w3c.dom.Element;
- class DomTest {
- public static void main (String[] args) {
- File f = new File(args[0]);
- Document document;
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder builder = factory.newDocumentBuilder();
- document = builder.parse(f);
- NodeList nl = document.getElementsByTagName("foo");
- System.out.println("How many? " + nl.getLength());
- // Obviously making a lot of assumptions about what's where
- Node foo = nl.item(0);
- System.out.println(foo.getChildNodes().item(0));
- // these catch clauses pasted in from some tutorial online
- } catch (SAXParseException spe) {
- // Error generated by the parser
- System.out.println("\n** Parsing error"
- + ", line " + spe.getLineNumber()
- + ", uri " + spe.getSystemId());
- System.out.println(" " + spe.getMessage() );
- // Use the contained exception, if any
- Exception x = spe;
- if (spe.getException() != null)
- x = spe.getException();
- x.printStackTrace();
- } catch (SAXException sxe) {
- // Error generated during parsing
- 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement