characters()
我有以下 XML 文件。为什么即使在应用验证之后也会出现空格
<Employee>
<Name>
James
</Name>
<Id>
11
</Id>
</Employee>
我正在尝试在标签之间显示文本。
public class MyHandler extends DefaultHandler {
boolean isName = false;
boolean isId = false;
@Override
public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
if (isName) {
System.out.println(new String(arg0, arg1, arg2));
isName = false;
}
if (isId) {
System.out.println(new String(arg0, arg1, arg2));
isId = false;
}
}
@Override
public void startElement(String arg0, String arg1, String arg2,
Attributes arg3) throws SAXException {
if (arg2.equalsIgnoreCase("Name")) {
isName = true;
}
if (arg2.equalsIgnoreCase("Id")) {
isId = true;
}
}
}
期望的输出:
James
11
实际输出:
James
11
为什么空间会在输出中出现?