Finding Closing XML tag

dsaddas

New Member
I have an method to print out just the contents between an opening and closing XML tag. I know my XML depth counter is the issue, but I can't figure out where to put it / fix it. Here is the XML.\[code\]<XMS> <Object> <Property> <Value>1</Value> </Property> </Object></XMS>\[/code\]Here is the Java code...\[code\] Boolean flag = false; while(reader.hasNext()) { eventType = reader.next(); if(getEventType(eventType) == "START_ELEMENT" && reader.getLocalName() == startTagName){ flag = true; depth++; System.out.println(reader.getLocalName() + " Depth = " + depth); } else if(getEventType(eventType) == "END_ELEMENT" && flag == true && (depth == 0)){ depth--; System.out.println(reader.getLocalName() + " Depth = " + depth + " BIG END"); break; } else if(getEventType(eventType) == "START_ELEMENT" && flag == true){ depth++; System.out.println(reader.getLocalName() + " Depth = " + depth); } else if(getEventType(eventType)== "END_ELEMENT" && flag == true){ depth--; System.out.println(reader.getLocalName() + " Depth = " + depth + " END"); } }}\[/code\]GetEventType simply returns the proper eventType (works properly). And here's the output from, the console.\[code\]Object Depth = 1Property Depth = 2Value Depth = 3Value Depth = 2 ENDProperty Depth = 1 ENDObject Depth = 0 ENDXMS Depth = -1 BIG END\[/code\]The issue is it should end on the last object when depth = 0 and stop before getting to XMS depth = -1. Any Advice?
 
Back
Top