You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsesDOMParser.java 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import org.xml.sax.SAXException;
  2. import java.io.InputStream;
  3. import java.io.IOException;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import org.xml.sax.InputSource;
  8. import org.apache.xerces.parsers.DOMParser;
  9. import java.io.PipedInputStream;
  10. import java.io.PipedOutputStream;
  11. import java.io.PrintWriter;
  12. import org.w3c.dom.Document;
  13. //public class UsesDOMParser {
  14. //
  15. // public static void main(String[] args) throws Exception {
  16. // PipedInputStream in = new PipedInputStream();
  17. // DOMParser parser = new DOMParser();
  18. // parser.parse(new InputSource(in));
  19. // System.out.println("All done!");
  20. // }
  21. //}
  22. public class UsesDOMParser {
  23. public static void main(String[] args) throws Exception {
  24. PipedInputStream in = new PipedInputStream();
  25. final PipedOutputStream out = new PipedOutputStream(in);
  26. Thread t = new Thread() {
  27. public void run() {
  28. try {
  29. String str =
  30. "<?xml version=\"1.0\"?>\n"
  31. + "<test>\n"
  32. + " <goes>\n"
  33. + " <here>yeah</here>\n"
  34. + " </goes>\n"
  35. + "</test>\n";
  36. PrintWriter o = new PrintWriter(out);
  37. o.println(str);
  38. o.flush();
  39. o.close();
  40. out.flush();
  41. out.close();
  42. } catch (Exception e) {
  43. String error =
  44. e.getClass().getName() + ": " + e.getMessage();
  45. throw new RuntimeException(error);
  46. }
  47. }
  48. };
  49. t.start();
  50. DOMParser parser = new DOMParser();
  51. parser.parse(new InputSource(in));
  52. Document doc = parser.getDocument();
  53. org.w3c.dom.Element root = doc.getDocumentElement();
  54. t.join();
  55. System.out.println("All done!");
  56. }
  57. } // end class