validating XML against schema using JDOM

Discussion in 'XML' started by srujana, Dec 29, 2005.

  1. srujana

    srujana Guest

    Hi

    I am validating an xml document against schema .
    when i am passing the xml file location as an input to the parse method

    String XmlDocumentUrl="E:/books.xml";
    parser.parse(XmlDocumentUrl);

    it is working fine.

    but how can i do that if i need to pass a xml document content instead
    of the file location.

    String XmlDocumentUrl="E:/books.xml";
    InputSource inputSource = new InputSource(new
    ByteArrayInputStream(XmlDocumentUrl.getBytes()));
    parser.parse(inputSource);

    The error is Cannot find the declaration of element 'Books'
    'Books' is my root element.

    Any help is greatly appreciated.
    Thanks.
     
    srujana, Dec 29, 2005
    #1
    1. Advertising

  2. srujana wrote:
    > String XmlDocumentUrl="E:/books.xml";
    > InputSource inputSource = new InputSource(new
    > ByteArrayInputStream(XmlDocumentUrl.getBytes()));


    This will create an input stream from the bytes in the string
    "E:/books.xml", not from the bytes in the file referenced by "E:/books.xml".

    --
    Johannes Koch
    Spem in alium nunquam habui praeter in te, Deus Israel.
    (Thomas Tallis, 40-part motet)
     
    Johannes Koch, Dec 31, 2005
    #2
    1. Advertising

  3. srujana

    Soren Kuula Guest

    srujana wrote:
    > Hi
    >
    > I am validating an xml document against schema .
    > when i am passing the xml file location as an input to the parse method
    >
    > String XmlDocumentUrl="E:/books.xml";
    > parser.parse(XmlDocumentUrl);
    >
    > it is working fine.
    >
    > but how can i do that if i need to pass a xml document content instead
    > of the file location.
    >
    > String XmlDocumentUrl="E:/books.xml";
    > InputSource inputSource = new InputSource(new
    > ByteArrayInputStream(XmlDocumentUrl.getBytes()));
    > parser.parse(inputSource);
    >
    > The error is Cannot find the declaration of element 'Books'
    > 'Books' is my root element.


    It could be something with base URI?

    With your first method, the parser has a chance to resolve relative URIs
    in your document, in order to load other resources, such as the schema.
    If the document contains this schema-location element thing (can't
    remember its name) with schema location "foo.xsd", the parser will
    resolve that to e:/foo.xsd . With your second method it doesn't have a
    chance, because it never heard about e:/ .

    You can probably set the base URI on the InputSource that you use,
    letting the parser know the base URI. If you don't want to use
    InputSources at all, you have to give the parser a schema location hint
    by calling some method on it, or make your own entity resolver and have
    to parser use that.

    Soren
     
    Soren Kuula, Dec 31, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. simon ames
    Replies:
    1
    Views:
    512
    Joerg Jooss
    Apr 11, 2004
  2. Mike P
    Replies:
    1
    Views:
    361
    Mike P
    Dec 6, 2004
  3. Ben Jessel
    Replies:
    0
    Views:
    418
    Ben Jessel
    Aug 5, 2004
  4. Jim Higson

    Validating DHTML against a schema

    Jim Higson, Dec 19, 2005, in forum: HTML
    Replies:
    4
    Views:
    430
    Toby Inkster
    Dec 19, 2005
  5. First_step_to_xml
    Replies:
    0
    Views:
    715
    First_step_to_xml
    Jun 28, 2006
Loading...

Share This Page