Advertisement
Guest User

Untitled

a guest
May 25th, 2010
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. import javax.xml.parsers.DocumentBuilder;
  2. import javax.xml.parsers.DocumentBuilderFactory;
  3. import javax.xml.parsers.FactoryConfigurationError;
  4. import javax.xml.parsers.ParserConfigurationException;
  5. import org.xml.sax.SAXException;
  6. import org.xml.sax.SAXParseException;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.DOMException;
  11. import org.w3c.dom.NodeList;
  12. import org.w3c.dom.Node;
  13. import org.w3c.dom.Element;
  14.  
  15.  
  16. class DomTest {
  17.     public static void main (String[] args) {
  18.         File f = new File(args[0]);
  19.         Document document;
  20.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  21.         try {
  22.             DocumentBuilder builder = factory.newDocumentBuilder();
  23.             document = builder.parse(f);
  24.             NodeList nl = document.getElementsByTagName("foo");
  25.             System.out.println("How many? " + nl.getLength());
  26.             // Obviously making a lot of assumptions about what's where
  27.             Node foo = nl.item(0);
  28.             System.out.println(foo.getChildNodes().item(0));
  29.         // these catch clauses pasted in from some tutorial online
  30.         } catch (SAXParseException spe) {
  31.           // Error generated by the parser
  32.           System.out.println("\n** Parsing error"
  33.             + ", line " + spe.getLineNumber()
  34.             + ", uri " + spe.getSystemId());
  35.           System.out.println("   " + spe.getMessage() );
  36.        
  37.           // Use the contained exception, if any
  38.           Exception  x = spe;
  39.           if (spe.getException() != null)
  40.             x = spe.getException();
  41.           x.printStackTrace();
  42.      
  43.         } catch (SAXException sxe) {
  44.           // Error generated during parsing
  45.           Exception  x = sxe;
  46.           if (sxe.getException() != null)
  47.             x = sxe.getException();
  48.           x.printStackTrace();
  49.      
  50.         } catch (ParserConfigurationException pce) {
  51.           // Parser with specified options can't be built
  52.           pce.printStackTrace();
  53.      
  54.         } catch (IOException ioe) {
  55.           // I/O error
  56.           ioe.printStackTrace();
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement