Implementing XML parser in Java for educational purposes

johnny_dio

New Member
I want to validate if a XML (in a String object) is well formed. Like this:\[code\]"<root> Hello StackOverflow! <a> Something here </a> Goodbye StackOverflow </root>"\[/code\]It should also validate attributes, but I'm kind of too far of that right now. I just want to make sure I have the logic right. Here's what I've got so far, but I'm stucked and I need some help.\[code\]public boolean isWellFormed( String str ){ boolean retorno = true; if ( str == null ) { throw new NullPointerException(); } else { this.chopTheElements( str ); this.chopTags(); } return retorno;}private void chopTags(){ for ( String element : this.elements ) { this.tags.add( element.substring( 1, element.length()-1 ) ); }}public void chopTheElements( String str ){ for ( int i = 0; i < str.length(); i++ ) { if ( str.charAt( i ) == '<' ) { elements.add( getNextToken( str.substring( i ) ) ); } }}private String getNextToken( String str ){ String retStr = ""; if ( str.indexOf( ">" ) != -1 ) { retStr = str.substring( 0, str.indexOf( ">" ) + 1 ); } return retStr;}\[/code\]So far I chopped the elements like "" in a list, and then the tags in another, like this: root, /root.But I don't know how to proceed or if I'm going in the right direction. I been asigned to solve this without regex.Any advice? I'm lost here. Thanks.
 
Back
Top