用dom怎么得到XML文件里的所有的节点和其子节点还有它的值啊?

zhangbo198294 2005-03-03 07:45:13
哪位大哥能告诉我啊!开发急用!
...全文
59 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
boy 2005-03-03
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.*;
//import com.ibm.xml.parsers.*;

/**
* A sample DOM writer. This sample program illustrates how to
* traverse a DOM tree.
*/

public class domOne
{
public void parseAndPrint(String uri)
{
Document doc = null;

try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new File(uri));
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: " + e);
}

// We've parsed the document now, so let's print it.

if (doc != null)
printDOMTree(doc);
}

/** Prints the specified node, recursively. */
public void printDOMTree(Node node)
{
int type = node.getNodeType();
switch (type)
{
// print the document element
case Node.DOCUMENT_NODE:
{
System.out.println("<?xml version=\"1.0\" ?>");
printDOMTree(((Document)node).getDocumentElement());
break;
}

// print element with attributes
case Node.ELEMENT_NODE:
{
System.out.print("<");
System.out.print(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++)
{
Node attr = attrs.item(i);
System.out.print(" " + attr.getNodeName() +
"=\"" + attr.getNodeValue() +
"\"");
}
System.out.print(">");

NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++)
printDOMTree(children.item(i));
}

break;
}

// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
System.out.print("&");
System.out.print(node.getNodeName());
System.out.print(";");
break;
}

// print cdata sections
case Node.CDATA_SECTION_NODE:
{
System.out.print("<![CDATA[");
System.out.print(node.getNodeValue());
System.out.print("]]>");
break;
}

// print text
case Node.TEXT_NODE:
{
System.out.print(node.getNodeValue());
break;
}

// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE:
{
System.out.print("<?");
System.out.print(node.getNodeName());
String data = node.getNodeValue();
{
System.out.print(" ");
System.out.print(data);
}
System.out.print("?>");
break;
}
}

if (type == Node.ELEMENT_NODE)
{
System.out.print("</");
System.out.print(node.getNodeName());
System.out.print('>');
}
} // printDOMTree(Node)

/** Main program entry point. */
public static void main(String argv[])
{
if (argv.length == 0)
{
System.out.println("Usage: java domOne uri");
System.out.println(" where uri is the URI of the XML document you want to print.");
System.out.println(" Sample: java domOne sonnet.xml");
System.exit(1);
}

domOne d1 = new domOne();
d1.parseAndPrint(argv[0]);
}
}
回复
julian_zzx 2005-03-03
用什么啊,记得应该是getChild()之类的吧,这些问题看看doc就都有了
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.6w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2005-03-03 07:45
社区公告
暂无公告