-
XML, Java and not using files
How do I use Java and XML using strings instead of files. I want to pass a
XML string into a method, process it, and pass it back out as a String.
Thanks,
Chris
-
Re: XML, Java and not using files
Chris,
An xml dom parser uses normally a stream for parsing the input and creating
the dom. When you want to do this with a string, just create a StringReader
stream (contructor takes a String) and give this StringReader to the parse
method. The parser will parse the String to a DOM.
The other way around is parser dependend. In most cases you find a print
method on your Document implementation (the exact name depends on wich
parser you are using) of your parser. Don't forget to specify you xml
version and encoding ! The Document object (interface) is coming from the
W3C and does not have a method for serializing to a String. If you want to
be parser independent then you have to deal with this Document and write you
own serialization method by a recursive algoritm.
Good luck,
Frank
chris <chrisc3616@hotmail.com> wrote in message
news:3a0b86b1$1@news.devx.com...
>
> How do I use Java and XML using strings instead of files. I want to pass a
> XML string into a method, process it, and pass it back out as a String.
>
> Thanks,
>
> Chris
-
Re: XML, Java and not using files
chris,
If u are asking for parsing xml strings instead of files than the answer
is use InputSource class to wrap the StringReader and then pass the InputSource
to the parser to parse the Xml String.
"chris" <chrisc3616@hotmail.com> wrote:
>
>How do I use Java and XML using strings instead of files. I want to pass
a
>XML string into a method, process it, and pass it back out as a String.
>
>Thanks,
>
>Chris
-
Re: XML, Java and not using files
I want to be able to parse strings in java, but am having trouble getting
it to work. Below is my function which I pass an unparsed xml string to.
It simply counts how many times a tag named "AP" is in the string. I keep
getting a SAX exception saying:
Problem with the SAX parser.
Illegal XML character: �.
public void makeAvailable(String xmlData)
{
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
System.out.println(xmlData);
StringReader sr = new StringReader(xmlData);
InputSource is = new InputSource(sr);
try{
SAXParser sax = factory.newSAXParser();
CountTagHandler cth = new CountTagHandler("AP");
sax.parse(is,cth);
System.out.println("The " + cth.tag + " appears " + cth.count + " times.");
} catch (ParserConfigurationException pce) {
System.out.println("Could not create the parser.");
System.out.println(pce.getMessage());
} catch (SAXException se) {
System.out.println("Problem with the SAX parser.");
System.out.println(se.getMessage());
} catch (IOException ioe) {
System.out.println("Error reading file.");
System.out.println(ioe.getMessage());
}
}
Here is the string that I am parsing:
<?xml version="1.0"?>
<AP>
<DESC>VIDEO</DESC>
<CMD>PLAY</CMD>
<CMD>STOP</CMD>
<CMD>RECORD</CMD>
<CMD>CH UP</CMD>
<CMD>CH DOWN</CMD>
</AP>
Can anyone see where I am going wrong?
Best regards,
Dean
"Frank Devliegher" <frank.devliegher@village.uunet.be> wrote:
>Chris,
>
>An xml dom parser uses normally a stream for parsing the input and creating
>the dom. When you want to do this with a string, just create a StringReader
>stream (contructor takes a String) and give this StringReader to the parse
>method. The parser will parse the String to a DOM.
>The other way around is parser dependend. In most cases you find a print
>method on your Document implementation (the exact name depends on wich
>parser you are using) of your parser. Don't forget to specify you xml
>version and encoding ! The Document object (interface) is coming from the
>W3C and does not have a method for serializing to a String. If you want
to
>be parser independent then you have to deal with this Document and write
you
>own serialization method by a recursive algoritm.
>
>Good luck,
>
>Frank
>chris <chrisc3616@hotmail.com> wrote in message
>news:3a0b86b1$1@news.devx.com...
>>
>> How do I use Java and XML using strings instead of files. I want to pass
a
>> XML string into a method, process it, and pass it back out as a String.
>>
>> Thanks,
>>
>> Chris
>
>
-
Re: XML, Java and not using files
Dean,
Which parser implementation are you using?
Anyway I did a search for �. Have a look at this link (http://lists.w3.org/Archives/Public/...tDec/0388.html)
and see if you can glean an answer from there. You may not see it but you
may have this character in your string. It also may have something to do
with unicode. Sorry I can't give a better answer - don't have time to dig
any further.
Mark
"Dean" <XmlRpc@cec.net.au> wrote:
>
>
>
>I want to be able to parse strings in java, but am having trouble getting
>it to work. Below is my function which I pass an unparsed xml string to.
>It simply counts how many times a tag named "AP" is in the string. I keep
>getting a SAX exception saying:
>Problem with the SAX parser.
>Illegal XML character: �.
>
>
> public void makeAvailable(String xmlData)
> {
> SAXParserFactory factory = SAXParserFactory.newInstance();
> factory.setValidating(false);
> System.out.println(xmlData);
> StringReader sr = new StringReader(xmlData);
> InputSource is = new InputSource(sr);
> try{
> SAXParser sax = factory.newSAXParser();
> CountTagHandler cth = new CountTagHandler("AP");
> sax.parse(is,cth);
> System.out.println("The " + cth.tag + " appears " + cth.count + "
times.");
> } catch (ParserConfigurationException pce) {
> System.out.println("Could not create the parser.");
> System.out.println(pce.getMessage());
> } catch (SAXException se) {
> System.out.println("Problem with the SAX parser.");
> System.out.println(se.getMessage());
> } catch (IOException ioe) {
> System.out.println("Error reading file.");
> System.out.println(ioe.getMessage());
> }
>
> }
>
>
>Here is the string that I am parsing:
><?xml version="1.0"?>
><AP>
> <DESC>VIDEO</DESC>
> <CMD>PLAY</CMD>
> <CMD>STOP</CMD>
> <CMD>RECORD</CMD>
> <CMD>CH UP</CMD>
> <CMD>CH DOWN</CMD>
></AP>
>
>Can anyone see where I am going wrong?
>
>Best regards,
>
>Dean
>
>
>
>
>
>
>
>
>
>
>"Frank Devliegher" <frank.devliegher@village.uunet.be> wrote:
>>Chris,
>>
>>An xml dom parser uses normally a stream for parsing the input and creating
>>the dom. When you want to do this with a string, just create a StringReader
>>stream (contructor takes a String) and give this StringReader to the parse
>>method. The parser will parse the String to a DOM.
>>The other way around is parser dependend. In most cases you find a print
>>method on your Document implementation (the exact name depends on wich
>>parser you are using) of your parser. Don't forget to specify you xml
>>version and encoding ! The Document object (interface) is coming from
the
>>W3C and does not have a method for serializing to a String. If you want
>to
>>be parser independent then you have to deal with this Document and write
>you
>>own serialization method by a recursive algoritm.
>>
>>Good luck,
>>
>>Frank
>>chris <chrisc3616@hotmail.com> wrote in message
>>news:3a0b86b1$1@news.devx.com...
>>>
>>> How do I use Java and XML using strings instead of files. I want to pass
>a
>>> XML string into a method, process it, and pass it back out as a String.
>>>
>>> Thanks,
>>>
>>> Chris
>>
>>
>
-
Re: XML, Java and not using files
Here is what I'm using...
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.*;
import org.w3c.dom.*;
/** XML Document Builder Factory. */
protected DocumentBuilderFactory m_DBF = DocumentBuilderFactory.newInstance();
public boolean XMLFromString( String strXML )
{
// Parse this XML string.
// Get byte array for strXML.
byte bytesXML[] = strXML.getBytes();
ByteArrayInputStream isXML = new ByteArrayInputStream( bytesXML );
org.w3c.dom.Document xmlState = null;
try
{
xmlState = m_DB.parse( isXML );
}
catch ( SAXException e )
{
return false;
}
catch ( java.io.IOException e )
{
return false;
}
Element objBooks = (Element)xmlState.getElementsByTagName("Books").item(
0 );
...
}
Tom
"chris" <chrisc3616@hotmail.com> wrote:
>
>How do I use Java and XML using strings instead of files. I want to pass
a
>XML string into a method, process it, and pass it back out as a String.
>
>Thanks,
>
>Chris
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks