Java and XML


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Java and XML

  1. #1
    John Perkins Guest

    Java and XML


    I just sat down the other night to work through the O'reilly book called "Java
    and XML", but I can't get the VERY FIRST example to work. I've downloaded
    and installed the Xerces parser from the site the book points to. I've even
    tried using the downloaded code from the O'Reilly site instead of the code
    that I've typed in myself. No matter what I get a parser exception that says
    "Error in parsing: File 'name of file' not found". I put some code in my
    test project to check that Java can really see the file and it can.

    Can anyone help me on this? What am I doing wrong?

    Thanks,
    John

    Here is the sample code....

    import java.io.File;

    import java.io.IOException;

    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;

    import org.apache.xerces.parsers.SAXParser;

    public class First extends java.lang.Object {

    public First() {
    }

    public void performDemo(String uri) {

    System.out.println("Parsing XML File: " + uri);

    try {
    // Instantiate a parser
    XMLReader parser = new SAXParser();

    // Parse the document
    parser.parse(uri);

    } catch (IOException e) {
    System.out.println("Error reading URI: " + e.getMessage());
    } catch (SAXException e) {
    System.out.println("Error in parsing: " + e.getMessage());
    }
    }

    public static void main (String args[]) {

    File file = new File("D:\\contents.xml");

    if (!file.exists()) {
    // This code doesn't fire in my tests
    System.out.println("File is missing");
    }

    try {
    String filename = new String(file.getPath());

    First test = new First();

    test.performDemo(filename);

    } catch (Exception e) {

    }
    }
    }

  2. #2
    John Perkins Guest

    Re: Java and XML


    Of course, I solve the problem and see my stupid error immediately after I've
    posted it to the world.

    I need to pass the file name in as... file:///D:/contents.xml

    -John


    "John Perkins" <perk_j@yahoo.com> wrote:
    >
    >I just sat down the other night to work through the O'reilly book called

    "Java
    >and XML", but I can't get the VERY FIRST example to work. I've downloaded
    >and installed the Xerces parser from the site the book points to. I've even
    >tried using the downloaded code from the O'Reilly site instead of the code
    >that I've typed in myself. No matter what I get a parser exception that

    says
    >"Error in parsing: File 'name of file' not found". I put some code in my
    >test project to check that Java can really see the file and it can.
    >
    >Can anyone help me on this? What am I doing wrong?
    >
    >Thanks,
    >John
    >
    >Here is the sample code....
    >
    >import java.io.File;
    >
    >import java.io.IOException;
    >
    >import org.xml.sax.SAXException;
    >import org.xml.sax.XMLReader;
    >
    >import org.apache.xerces.parsers.SAXParser;
    >
    >public class First extends java.lang.Object {
    >
    > public First() {
    > }
    >
    > public void performDemo(String uri) {
    >
    > System.out.println("Parsing XML File: " + uri);
    >
    > try {
    > // Instantiate a parser
    > XMLReader parser = new SAXParser();
    >
    > // Parse the document
    > parser.parse(uri);
    >
    > } catch (IOException e) {
    > System.out.println("Error reading URI: " + e.getMessage());
    > } catch (SAXException e) {
    > System.out.println("Error in parsing: " + e.getMessage());
    > }
    > }
    >
    > public static void main (String args[]) {
    >
    > File file = new File("D:\\contents.xml");
    >
    > if (!file.exists()) {
    > // This code doesn't fire in my tests
    > System.out.println("File is missing");
    > }
    >
    > try {
    > String filename = new String(file.getPath());
    >
    > First test = new First();
    >
    > test.performDemo(filename);
    >
    > } catch (Exception e) {
    >
    > }
    > }
    >}



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center