求助求助~~~~仔细阅读以下JAVA文档,写出运行后产生的xml文件结构和数据。
publicclassCreateXml{publicstaticvoidmain(Stringargs[]){try{Stringname[]={"钱二","张三"};...
public class CreateXml{
public static void main(String args[]){
try{
String name[]={"钱二","张三"};
String id[]={"094831071","094831072"};
String hobby[]={"唱歌","上网"};
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.newDocument();
document.setXmlVersion("1.0");
Element root=document.createElement("员工信息");
document.appendChild(root);
for(int k=0;k<name.length;k++){
Element ep=document.createElement("员工");
ep.setAttribute("工号",id[k]);
root.appendChild(ep);
Element ename=document.createElement("名字");
ename.appendChild(document.createTextNode(name[k]));
stu.appendChild(ename);
Element ehobby=document.createElement("爱好");
ehobby.appendChild(document.createTextNode(hobby[k]));
stu.appendChild(ehobby);
}
TransformerFactory transFactory=TransformerFactory.newInstance();
Transformer transformer=transFactory.newTransformer();
DOMSource domSource=new DOMSource(document);
File file=new File("员工信息表.xml");
FileOutputStream out=new FileOutputStream(file);
StreamResult xmlResult=new StreamResult(out);
transformer.transform(domSource,xmlResult);
}
catch(Exception e){
System.out.println(e);
}}} 展开
public static void main(String args[]){
try{
String name[]={"钱二","张三"};
String id[]={"094831071","094831072"};
String hobby[]={"唱歌","上网"};
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.newDocument();
document.setXmlVersion("1.0");
Element root=document.createElement("员工信息");
document.appendChild(root);
for(int k=0;k<name.length;k++){
Element ep=document.createElement("员工");
ep.setAttribute("工号",id[k]);
root.appendChild(ep);
Element ename=document.createElement("名字");
ename.appendChild(document.createTextNode(name[k]));
stu.appendChild(ename);
Element ehobby=document.createElement("爱好");
ehobby.appendChild(document.createTextNode(hobby[k]));
stu.appendChild(ehobby);
}
TransformerFactory transFactory=TransformerFactory.newInstance();
Transformer transformer=transFactory.newTransformer();
DOMSource domSource=new DOMSource(document);
File file=new File("员工信息表.xml");
FileOutputStream out=new FileOutputStream(file);
StreamResult xmlResult=new StreamResult(out);
transformer.transform(domSource,xmlResult);
}
catch(Exception e){
System.out.println(e);
}}} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询