请问通过dom4j是不是不能采用sax方式解析xml,就是基于事件的那种方式
3个回答
展开全部
dom4j也有sax的解析方式的 , 具体请看如下代码 ;
public static void ReadXMLBySAX() throws DocumentException
{
List list;
File f = new File("hope.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(f); // read(File file);Reads a Document from the given File
// 读取内容
list = doc.selectNodes("//content");
Iterator i_content = list.iterator();
if (i_content.hasNext())
{
Element element = (Element) i_content.next();
String content = element.getText();
System.out.println("内容是:" + content);
}
// 读取标题和时间
list = doc.selectNodes("//attr/@name");
if (list != null) {
Iterator iter = list.iterator();
while (iter.hasNext())
{
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("标题"))
{
System.out.println("标题:" + attribute.getParent().getText());
break;
}
}
}
}
public static void ReadXMLBySAX() throws DocumentException
{
List list;
File f = new File("hope.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(f); // read(File file);Reads a Document from the given File
// 读取内容
list = doc.selectNodes("//content");
Iterator i_content = list.iterator();
if (i_content.hasNext())
{
Element element = (Element) i_content.next();
String content = element.getText();
System.out.println("内容是:" + content);
}
// 读取标题和时间
list = doc.selectNodes("//attr/@name");
if (list != null) {
Iterator iter = list.iterator();
while (iter.hasNext())
{
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("标题"))
{
System.out.println("标题:" + attribute.getParent().getText());
break;
}
}
}
}
参考资料: 新浪 hope的博客
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询