使用制表符为何输出的列不能对齐(如图)

侠路相逢 2016-10-18 12:10:13
			
package cn.jbit.exercise14_4;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlDom {

public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("D:\\student.xml");
NodeList nl = doc.getElementsByTagName("student");
String id = null;
//String nodeName = null;
String nodeValue = null;
System.out.println("XML文档中共有"+nl.getLength()+"个学生:");
System.out.println("ID\t\tName\t\tAge\t\tSchool");
for(int i = 0; i < nl.getLength(); i++){
StringBuffer sb = new StringBuffer();
Node stu = nl.item(i);
Element elmt = (Element) stu;
id = elmt.getAttribute("id");
sb = sb.append(id+"\t\t");
for(Node node = stu.getFirstChild(); node !=null; node = node.getNextSibling()){
if(node.getNodeType() == Node.ELEMENT_NODE){
//nodeName = node.getNodeName();
nodeValue = node.getFirstChild().getNodeValue();
sb.append(nodeValue+"\t\t");
}
}
System.out.println(sb.toString());
}
}
catch(Exception e){
e.printStackTrace();
}

}

}


...全文
1288 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 qq_32031745 的回复:
\t只是说明在该处插入一个4长度的空格,并不能帮助你对齐,就像在原地打了4个空格一样。 如果你需要对齐可以用printf();格式化输出。如: System.out.printf("%10s%10s%10s",s1,s2,s3); %10s代表此处插入一个占位10长度的字符串。
侠路相逢 2016-10-18
  • 打赏
  • 举报
回复
引用 1楼一度人生 的回复:
\t只是说明在该处插入一个4长度的空格,并不能帮助你对齐,就像在原地打了4个空格一样。 如果你需要对齐可以用printf();格式化输出。如: System.out.printf("%10s%10s%10s",s1,s2,s3); %10s代表此处插入一个占位10长度的字符串。
非常感谢
肃穆丶 2016-10-18
  • 打赏
  • 举报
回复
mark
一度人生 2016-10-18
  • 打赏
  • 举报
回复
\t只是说明在该处插入一个4长度的空格,并不能帮助你对齐,就像在原地打了4个空格一样。 如果你需要对齐可以用printf();格式化输出。如: System.out.printf("%10s%10s%10s",s1,s2,s3); %10s代表此处插入一个占位10长度的字符串。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧