100分,请您帮我解析一下这个简单的XML。谢谢!

liujava9 2006-03-22 09:12:16
<VInfo ID='abc' Name='other'>
<I User="KK" N="oo"/>
<I User="KK" N="oo"/>
</VInfo>


请问如何取出下列属性的名称?
VInfo ID Name
...全文
87 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kill8108 2006-03-22
  • 打赏
  • 举报
回复
w3c的dom作相应的修改,查看API接口就可看到很多的相似的方法与类的了!
思路:xml---document---element--attrubute;
kill8108 2006-03-22
  • 打赏
  • 举报
回复
我也来写一个参考下:
import org.jdom.input.*;
import org.jdom.*;
import java.util.*;
import java.io.*;
public class XMLExample2
{
public static void main(String[] args)
{

SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(new FileInputStream("text1.xml"));
Element root = doc.getRootElement();
System.out.println("节点名:"+root.getName());
List lProperty = root.getAttributes();
Iterator it = lProperty.iterator();
while(it.hasNext())
{
System.err.println(" : "+root.getAttribute(((Attribute)it.next()).getName()).getValue());
}
List ro = root.getChildren();
Iterator it1 = ro.iterator();
while(it1.hasNext())
{
Element el = (Element)it1.next();
lProperty = el.getAttributes();
Iterator it2 = lProperty.iterator();
while(it2.hasNext())
{
System.err.println(" : "+el.getAttribute(((Attribute)it2.next()).getName()).getValue());
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}

}
wilowind 2006-03-22
  • 打赏
  • 举报
回复
初次接触xml解析,没用过dom。
看看他的帮助,有没有相关方法。我这只有jdom和dom4j
liujava9 2006-03-22
  • 打赏
  • 举报
回复
用w3c的dom好解析吗?
wilowind 2006-03-22
  • 打赏
  • 举报
回复
import java.io.File;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;


public class TestParseAttrName {
public static void main(String arge[]) {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File("D:\\test.xml"));
Element foo = doc.getRootElement();
System.out.println("节点名:"+foo.getName());
List attrList = foo.getAttributes();
for(int i=0;i<attrList.size();i++)
{
System.out.println("节点属性["+i+"]:"+((Attribute)attrList.get(i)).getName());
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}

------------------------------
输出

节点名:VInfo
节点属性[0]:ID
节点属性[1]:Name

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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