X
Xavier Seneque
Hi everybody,
i wish to create a server that receives some data/queries from an xml
formatted stream, using java xml parsers
but i got some problems
here are some generaly thoughts about the project :
the DTD describing the format of the stream that the server will receive
is an external DTD and is only on the server... right ?
then, in no way the client should be able to specify a DTD or write an
internal DTD within its stream... the server should arbitrary use its
DTD to parse the stream
so i started to make some code to parse a well formed stream wich works :
first, here is my simple DTD, seems valid isn't it ?
//begin DTD
<?xml version='1.0' encoding='UTF-8' ?>
<!ELEMENT session (query_get_im_srv)>
<!ELEMENT query_get_im_srv (user)>
<!ELEMENT user (#PCDATA)>
//end DTD
so the server should receive a message that looks like this :
<session><query_get_im_srv><user>foo</user></query_get_im_srv></session>
here is the code i use to parse it, it works quite well.... how do you
like it ?
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
saxParser = factory.newSAXParser();
saxParser.parse(new InputSource(stream),handler);
// stream is the XML stream from the client
// handler a class that extends DefaultHandler which overloads the
callbacks from the events
i've read somewhere on the net that in order to validate a stream, i
should write something like this, but i'm not sure at all !!
SchemaFactory sf =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema s = sf.newSchema( new File("mydtd.dtd") );
Validator validator = s.newValidator();
validator.validate(xmlSource);
but it gets me some pretty exception :
--> org.xml.sax.SAXParseException: The markup in the document preceding
the root element must be well-formed.
and i don't really know where it comes from ... is my dtd not valid ?
anyhow, do you think i'm in the right way ? or do you thing i should use
something else ? some links on the topic ? any advice is welcome,
because for parsing files their is plenty of info, but streams... not a
lot
thanks !
xavier seneque
i wish to create a server that receives some data/queries from an xml
formatted stream, using java xml parsers
but i got some problems
here are some generaly thoughts about the project :
the DTD describing the format of the stream that the server will receive
is an external DTD and is only on the server... right ?
then, in no way the client should be able to specify a DTD or write an
internal DTD within its stream... the server should arbitrary use its
DTD to parse the stream
so i started to make some code to parse a well formed stream wich works :
first, here is my simple DTD, seems valid isn't it ?
//begin DTD
<?xml version='1.0' encoding='UTF-8' ?>
<!ELEMENT session (query_get_im_srv)>
<!ELEMENT query_get_im_srv (user)>
<!ELEMENT user (#PCDATA)>
//end DTD
so the server should receive a message that looks like this :
<session><query_get_im_srv><user>foo</user></query_get_im_srv></session>
here is the code i use to parse it, it works quite well.... how do you
like it ?
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
saxParser = factory.newSAXParser();
saxParser.parse(new InputSource(stream),handler);
// stream is the XML stream from the client
// handler a class that extends DefaultHandler which overloads the
callbacks from the events
i've read somewhere on the net that in order to validate a stream, i
should write something like this, but i'm not sure at all !!
SchemaFactory sf =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema s = sf.newSchema( new File("mydtd.dtd") );
Validator validator = s.newValidator();
validator.validate(xmlSource);
but it gets me some pretty exception :
--> org.xml.sax.SAXParseException: The markup in the document preceding
the root element must be well-formed.
and i don't really know where it comes from ... is my dtd not valid ?
anyhow, do you think i'm in the right way ? or do you thing i should use
something else ? some links on the topic ? any advice is welcome,
because for parsing files their is plenty of info, but streams... not a
lot
thanks !
xavier seneque