Hello All, I am trying to Parse an XML file which i converted into string using Java IO files. Now the XML String has to be parsed by using Java Application Program, that is without using DOM or SAX parsers. How can i do it without using DOM or SAX. If you can suggest me some tutorial or code snipped i can develop the idea. Your reply is very much appreciated.
Is there a reason why you are trying to parse XML without using a parser? No matter, I'll assume there is. Your only route in this situation would be to...write a parser. You might condiser the java.util.regex package if you must do this.
I am assuming this is some type of homework assignment, which is why you can't use a parser...
If you know what xml tags you are looking for, you can use the String method indexOf to locate the tag. Then use indexOf again to find the end tag and everything in between the tags is your data. Using the two indexes you found you can use the String substring method to pull the text out from betwen the tags.
Thanks Paul, It seems using regex is the best way to parse without SAX or DOM. I searched the net but i couldnt get code snippet of how to do this. If you can provide me with the code i can learn easily. Just give me a sample to head start in this topic.
Dear Nick, I want to parse the XML file without using DOM or SAX or any other API. I just want to parse the file manually. To do this i have converted the XML file to String using java io. Now i have to parse this String(contains XML) without using SAX or DOM.
What exactly do you have to do? Parsing an XML file means reading it. Parsers don't do much of anything by themselves other than check for well-formedness (if there is such a word). Are you supposed to find a particular piece of data and display it in some way? Or are you supposed to determine if the XML file is well-formed?
It really is easy to parse simple cases yourself. I did this in REXX not long ago cause I didn't have a parser. If your XML is as shown before ...
The first character must be < Use indexOf to find > Use substring to get the tag Use indexOf to find </tag> Use substring to get the value between the open & close tags Store tag & value somewhere The next character should be < or end of input That looks familiar, go back to line 2.
If you get attributes, namespaces and more advanced XML things get trickier, but your example should be easy enough.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi