■■如何解析一段XML的字符串""?高分!!!!!■■

yizhao 2005-04-07 11:32:00
String xmlString= "<area id="12345" name="test" top="10"/>";
Document document = null;

if (xmlString != null && xmlString.length() > 0) {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;

try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}

try {
StringReader rd = new StringReader(xmlString);
InputSource in = new InputSource(rd);
document = db.parse(in);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException saxe) {
saxe.printStackTrace();
}


已经获得了document

如何获得其中的id、name、top的值?
...全文
122 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yizhao 2005-04-07
  • 打赏
  • 举报
回复
请不要只说,请调试看看

如果是XML文件我早就搞好了

可能STRING的字符串处理有一定的特殊性

请按代码调试一下看看,谢谢
飞行的兔子 2005-04-07
  • 打赏
  • 举报
回复
既然得到了document,就直接用getElementById(String id)取得相应的element,
取得了Element,再用Element的getNodeAttribute(String name)取得相应的属性!
看看Dom的api
benu 2005-04-07
  • 打赏
  • 举报
回复
你要得到什么?
yizhao 2005-04-07
  • 打赏
  • 举报
回复
String xmlString= "<area id=\"12345\" name=\"test\" top=\"10\"/>";

补充一下,这句需要转义

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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