Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Parsing XML from a String

Thread view: 
miki miska - 25 May 2006 12:46 GMT
Hi !

I am using the following code to parse XML from a file:
...
   DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
   f.setValidating( false );
   Document d = f.newDocumentBuilder().parse(new File( "demo.xml" ));
   rootNode = doc.getDocumentElement().getTagName();
...

Now, parse has option also to take a Stream as an argument. What I want
is, to parse me an XML code from a String. Is there any way to
Streamizie a string ?

thx
Boris Gorjan - 25 May 2006 13:01 GMT
> Hi !
>
[quoted text clipped - 11 lines]
>
> thx

String -> byte[] (according to some charset) -> ByteArrayInputStream
Mike Schilling - 25 May 2006 15:53 GMT
>> Hi !
>>
[quoted text clipped - 13 lines]
>
> String -> byte[] (according to some charset) -> ByteArrayInputStream

But the last thing you want to do is convert characters to bytes so that the
XML parser can convert them back to characters.
Boris Gorjan - 29 May 2006 15:48 GMT
>>> Hi !
>>>
[quoted text clipped - 15 lines]
> But the last thing you want to do is convert characters to bytes so that the
> XML parser can convert them back to characters.

I agree. But miki miska asked how to "Streamizie a string", so that he/she could
use it in .parse(InputStream). This goes to show how flawed the API is. Instead
of simply having .parse(Reader), one has to take one extra step:
.parse(InputSource). Moreover, .parse(String) doesn't quite do what one might
expect. As if java.lang.URI wouldn't exist and String had to take its place.
What can I say, some (Sun-?)mastermind's brainfart, I guess.
Mike Schilling - 29 May 2006 16:16 GMT
>>>> Hi !
>>>>
[quoted text clipped - 19 lines]
> could use it in .parse(InputStream). This goes to show how flawed the API
> is.

Yes.  People often ask a question that's too specific.

>  Instead of simply having .parse(Reader), one has to take one extra step:
> .parse(InputSource). Moreover, .parse(String) doesn't quite do what one
> might expect. As if java.lang.URI wouldn't exist and String had to take
> its place. What can I say, some (Sun-?)mastermind's brainfart, I guess.
miki miska - 03 Jul 2006 14:06 GMT
> >>>> Hi !
> >>>>
[quoted text clipped - 26 lines]
> > might expect. As if java.lang.URI wouldn't exist and String had to take
> > its place. What can I say, some (Sun-?)mastermind's brainfart, I guess.

Well, Miki miska thanks to all informations he got :)
Frank Langelage - 25 May 2006 13:06 GMT
> Hi !
>
[quoted text clipped - 9 lines]
> is, to parse me an XML code from a String. Is there any way to
> Streamizie a string ?

StringReader reader = new StringReader( s );
InputSource inputSource = new InputSource( reader );
Document doc = builder.parse( inputSource );
reader.close();
Mike Schilling - 25 May 2006 15:52 GMT
> Hi !
>
[quoted text clipped - 9 lines]
> is, to parse me an XML code from a String. Is there any way to
> Streamizie a string ?

See the DocumentBuilder.parse(InputSource) overload.  An InputSource can be
based on a Reader, including a StringReader.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.