java - Parsing XML file with preserving information about the line number -


i creating tool analyzes xml files (xhtml files precise). purpose of tool not validate xml structure, check value of attributes.

so created own org.xml.sax.helpers.defaulthandler handle events during xml parsing. 1 of requirements have information current line number. decided add org.xml.sax.helpers.locatorimpl own defaulthandler. solves problems, except 1 regarding xml attributes.

let's take example:

<rootnode>     <foo att1="val1"/>     <bar att2="val2"          answertoeverything="43"          att3="val3"/> </rootnode> 

one of rules indicates if attribute answertoeverything defined on node bar, value should not different 42.

when encountering such xml, tool should detect error. want give precise error message user, such as:

error in file "foo.xhtml", line #4: answertoeverything allow "42" value.

my parser must able keep line number during parsing, even attributes. if consider following implementation own defaulthandler class:

public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception {     system.out.println("start element <" + qname + ">" + x());     (int = 0; < attributes.getlength(); i++) {         system.out.println("att '" + attributes.getqname(i) + "' = '" + attributes.getvalue(i) + "' @ " + locator.getlinenumber() + ":" + locator.getcolumnnumber());     } } 

then node >bar>, display following output:

start element @ 5:23
att 'att2' = 'val2' @ 5:23
att 'answertoeverything' = '43' @ 5:23
att 'att3' = 'val3' @ 5:23

as can see, line number wrong because parser consider whole node, including attributes 1 block.

ideally, if interface contenthandler have defined startattribute , startelementbeforereadingattributes methods, wouldn't have problem here :o)

so question how can solve problem?

for information, using java 6

ps: maybe title question java sax parsing attributes parsing events, or that...

i think way implement create own inputstream (or reader) counts lines , somehow communicates sax handler. have not tried implement myself believe possible. wish luck , glad if succeed , post results here.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -