domparser java_使用DOMParser 解析xml的Java代码详解

import java.io.*;

import java.net.*;

import org.w3c.dom.*;

import org.w3c.dom.Node.*;

import oracle.xml.parser.v2.*;

/**

* -----------------------------------------------------------------------------

* Demonstrate how to use DOM.

*

* @version 1.0

* @author Jeffrey M. Hunter ([email protected])

* @author http://www.idevelopment.info

* -----------------------------------------------------------------------------

*/

public class DOMExample {

/*

* +---------------------------------------------+

* | METHOD: main |

* +---------------------------------------------+

*/

static public void main(String[] argv) {

try {

if (argv.length != 1) {

// must pass in the name of the XML file

System.err.println("Usage: java DOMExample filename");

System.exit(1);

}

// Get an instance of the parser

DOMParser parser = new DOMParser();

// Generate a URL from the filename

URL url = createURL(argv[0]);

// Set various parser options; validation on,

// warnings shown, error stream set to stderr.

parser.setErrorStream(System.err);

parser.setValidationMode(true);

parser.showWarnings(true);

// parse the document

parser.parse(url);

// Obtain the document

XMLDocument doc = parser.getDocument();

// print document elements

System.out.print("The elements are: ");

printElements(doc);

// print document elements attributes

System.out.println("The attributes of each element are: ");

printElementAttributes(doc);

} catch (Exception e) {

System.out.println(e.toString());

}

}

/*

* +---------------------------------------------+

* | METHOD: printElements |

* +---------------------------------------------+

*/

static void printElements(Document doc) {

NodeList nodelist = doc.getElementsByTagName("*");

Node node;

for (int i=0; i

node = nodelist.item(i);

System.out.print(node.getNodeName() + " ");

}

System.out.println();

}

/*

* +---------------------------------------------+

* | METHOD: printElementAttributes |

* +---------------------------------------------+

*/

static void printElementAttributes(Document doc) {

NodeList nodelist = doc.getElementsByTagName("*");

Node node;

Element element;

NamedNodeMap nnm = null;

String attrname;

String attrval;

int i, len;

len = nodelist.getLength();

for (int j=0; j < len; j++) {

element = (Element)nodelist.item(j);

System.out.println(element.getTagName() + ":");

nnm = element.getAttributes();

}

if (nnm != null) {

for (i=0; i

node = nnm.item(i);

attrname = node.getNodeName();

attrval = node.getNodeValue();

System.out.println(" " + attrname + " = " + attrval);

}

}

System.out.println();

}

/*

* +---------------------------------------------+

* | METHOD: createURL |

* +---------------------------------------------+

*/

static URL createURL(String filename) {

URL url = null;

try {

url = new URL(filename);

} catch (MalformedURLException ex) {

try {

File f = new File(filename);

url = f.toURL();

} catch (MalformedURLException e) {

System.out.println("Cannot create URL for: " + filename);

System.exit(0);

}

}

return url;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值