我也初学xml,今后多多指点!
解决方案 »
- 由一般的WEB开发转向手机方向的WEB开发需要哪些技术知识储备
- 面试题,看你能做出多少
- 请问有没高人知道这个错误怎么解决 很急 找好久了
- 各位大侠,谁有阴历转农历,农历转阴历的java源码?在下多谢啦!
- Entity的自动创建表问题。
- jboss&tomecat问题
- ssh框架搭建问题
- DynaValidatorForm验证,errors.head能够显示,但error.username.required却不能显示
- 收集近两年倒闭的软件公司,在南京
- JBuilder中编译EJB组件时候出错,在线求教。。。
- 我安装了jb6,菜单项 file->new->EJB 2.0 Bean designer为什么不能激活呢
- 哪里有关于J2EE,EJB的电子书下载
txml.xml文件,注意这里的方法,不然jdk自己带的那些解析器会
解析出来一些乱七八糟的东西。
<?xml version="1.0" standalone="yes"?>
<GREETING id="test">ZosaTapo<person>Reic Yang</person></GREETING>java程序:
import java.io.*;
import java.lang.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.w3c.dom.*;
class txml
{
private static final String FILE_PATH="txml.xml";
private Document doc=null;
private DocumentBuilder docb=null;
private DocumentBuilderFactory docbf=null;
private Element root=null;
public void loadDocument()
{
try
{
docbf=DocumentBuilderFactory.newInstance();
docb=docbf.newDocumentBuilder();
doc=docb.parse(new File(FILE_PATH));
root=doc.getDocumentElement();
}
catch(ParserConfigurationException e){e.printStackTrace();}
catch(SAXException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
public Element getRoot()
{
return root;
}
public Document getDoc()
{
return doc;
}
public static void spaces(int depth)
{
//print prefix space to show level
for(int d=0;d<depth;d++)
{
System.out.print(" ");
}
//System.out.println();
}
public static void printTree(Node root,int depth)
{
System.out.println();
//print attributes
if(root.hasAttributes())
{
System.out.println("******Node Attributes List******");
NamedNodeMap namednodemap=root.getAttributes();
for(int a=0;a<namednodemap.getLength();a++)
{
spaces(depth);
System.out.println(namednodemap.item(a).getNodeName()
+" : "+namednodemap.item(a).getNodeValue());
}
}
//print nodes
NodeList nodelist=root.getChildNodes();
spaces(depth);
System.out.println("******Node ChildNodes List******");
spaces(depth);
System.out.println("Nodes Total:"+nodelist.getLength());
for(int n=0;n<nodelist.getLength();n++)
{
Node node=nodelist.item(n);
spaces(depth);
System.out.print("Node Type :"+node.getNodeType());
System.out.print(" Node Name :"+node.getNodeName());
System.out.print(" Node Value :"+node.getNodeValue());
System.out.println();
if(node.getNodeType()==1)
{
printTree(node,depth+1);
}
}
System.out.println();
}
public static void main(String args[])
{
txml t=new txml();
t.loadDocument();
Document doc=t.getDoc();
Element root=t.getRoot();
System.out.println("+++++++++++++++++++++++++++++++++");
System.out.println("+ Node List +");
System.out.println("+++++++++++++++++++++++++++++++++");
printTree(root,0);
}
}