Problems: The eXtended Markup Language (XML) is a widely used format for describ
ID: 3739539 • Letter: P
Question
Problems: The eXtended Markup Language (XML) is a widely used format for describing structured data. An XML document is a collection of matching start-tags and end-tags. A start-tag is a string of alphabetic characters preceded by a . For example is a start-tag of type "foo". An end-tag is an alphabetic string preceded by. For example is an end-tag of type "foo Well-formed XML requires that start-tags and end-tags match (i.e. have the same type.) a" outer tags and "binner t No a" end-tags does not match start-tag cachocac/axchoc cablex cbaker> No Yes al tags balance end-tag does not match start- C"baker) Le, the tag names The algorithm to determine if the start and end-tags balance uses a Stack data structure to keep track of previously read start-tags. The algorithm is 1. Read the input until the beginning of a tag is detected. (i.e. tags begin withExplanation / Answer
private boolean isTagBalanced(String s) {
Stack stack = new Stack();
for(int i = 0; i < s.length(); i++) {
String start = s.substring(0,2);
char d = s.charAt(i);
if(d == '<' && !start.equals("</")) {
stack.push(c);
}
else if(start.equals("</")) {
if(stack.isEmpty()) {
return false;
}
else {
char c2 = (char) stack.pop();
}
}
}
if(stack.isEmpty()) {
System.out.println("Tag is complete");
return true;
}
return false;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.