2个回答
展开全部
dom4j中,使用Element.attributes方法可以获取到节点的属性,而使用elements则可以获取相应的子节点
比如:
Element root = doc.getRootElement();
List attrList = root.attributes();
for (int i = 0; i < attrList.size(); i++) {
//属性的取得
Attribute item = (Attribute)attrList.get(i);
System.out.println(item.getName() + "=" + item.getValue());
}
List childList = root.elements();
for (int i = 0; i < childList.size(); i++) {
//子节点的操作
Element it = (Element) childList.get(i);
//对子节点进行其它操作...
}
比如:
Element root = doc.getRootElement();
List attrList = root.attributes();
for (int i = 0; i < attrList.size(); i++) {
//属性的取得
Attribute item = (Attribute)attrList.get(i);
System.out.println(item.getName() + "=" + item.getValue());
}
List childList = root.elements();
for (int i = 0; i < childList.size(); i++) {
//子节点的操作
Element it = (Element) childList.get(i);
//对子节点进行其它操作...
}
展开全部
java读取xml节点元素,主要使用java提供的解析xml的工具类SAXParserFactory,如下代码:package
xml.xmlreader;import
java.io.File;import
java.net.URL;import
java.util.Properties;import
javax.xml.parsers.SAXParser;import
javax.xml.parsers.SAXParserFactory;public
class
CFGParser
{//解析xml文件的工具类
private
Properties
props;
public
Properties
getProps()
{
return
props;
}
public
void
setProps(Properties
props)
{
this.props
=
props;
}
public
void
parse(String
filename)
throws
Exception
{
CFGHandler
handler
=
new
CFGHandler();
SAXParserFactory
factory
=
SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
SAXParser
parser
=
factory.newSAXParser();
URL
confURL
=
super.getClass().getClassLoader().getResource(filename);
if
(confURL
==
null)
{
System.out.println("Can't
find
configration
file.");
return;
}
try
{
parser.parse(confURL.toString(),
handler);
this.props
=
handler.getProps();
}
finally
{
factory
=
null;
parser
=
null;
handler
=
null;
}
}
public
void
parseFile(String
filename)
throws
Exception
{
CFGHandler
handler
=
new
CFGHandler();
SAXParserFactory
factory
=
SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
SAXParser
parser
=
factory.newSAXParser();
File
f
=
new
File(filename);
if
((f
==
null)
||
(!f.exists()))
return;
try
{
parser.parse(f,
handler);
this.props
=
handler.getProps();
}
finally
{
factory
=
null;
parser
=
null;
handler
=
null;
}
}}package
xml.xmlreader;import
java.util.Properties;import
org.xml.sax.Attributes;import
org.xml.sax.SAXException;import
org.xml.sax.helpers.DefaultHandler;public
class
CFGHandler
extends
DefaultHandler{
private
Properties
props;
private
String
currentSet;
private
String
currentName;
private
StringBuffer
currentValue
=
new
StringBuffer();
public
CFGHandler()
{
this.props
=
new
Properties();
}
public
Properties
getProps()
{
return
this.props;
}
public
void
startElement(String
uri,
String
localName,
String
qName,
Attributes
attributes)
throws
SAXException
{
this.currentValue.delete(0,
this.currentValue.length());
this.currentName
=
qName;
}
public
void
characters(char[]
ch,
int
start,
int
length)
throws
SAXException
{
this.currentValue.append(ch,
start,
length);
}
public
void
endElement(String
uri,
String
localName,
String
qName)
throws
SAXException
{
this.props.put(qName.toLowerCase(),
this.currentValue.toString().trim());
}}xml文件
6
10
23:00
12:00
18:00jsp获取各个节点的值:
xml.xmlreader;import
java.io.File;import
java.net.URL;import
java.util.Properties;import
javax.xml.parsers.SAXParser;import
javax.xml.parsers.SAXParserFactory;public
class
CFGParser
{//解析xml文件的工具类
private
Properties
props;
public
Properties
getProps()
{
return
props;
}
public
void
setProps(Properties
props)
{
this.props
=
props;
}
public
void
parse(String
filename)
throws
Exception
{
CFGHandler
handler
=
new
CFGHandler();
SAXParserFactory
factory
=
SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
SAXParser
parser
=
factory.newSAXParser();
URL
confURL
=
super.getClass().getClassLoader().getResource(filename);
if
(confURL
==
null)
{
System.out.println("Can't
find
configration
file.");
return;
}
try
{
parser.parse(confURL.toString(),
handler);
this.props
=
handler.getProps();
}
finally
{
factory
=
null;
parser
=
null;
handler
=
null;
}
}
public
void
parseFile(String
filename)
throws
Exception
{
CFGHandler
handler
=
new
CFGHandler();
SAXParserFactory
factory
=
SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
SAXParser
parser
=
factory.newSAXParser();
File
f
=
new
File(filename);
if
((f
==
null)
||
(!f.exists()))
return;
try
{
parser.parse(f,
handler);
this.props
=
handler.getProps();
}
finally
{
factory
=
null;
parser
=
null;
handler
=
null;
}
}}package
xml.xmlreader;import
java.util.Properties;import
org.xml.sax.Attributes;import
org.xml.sax.SAXException;import
org.xml.sax.helpers.DefaultHandler;public
class
CFGHandler
extends
DefaultHandler{
private
Properties
props;
private
String
currentSet;
private
String
currentName;
private
StringBuffer
currentValue
=
new
StringBuffer();
public
CFGHandler()
{
this.props
=
new
Properties();
}
public
Properties
getProps()
{
return
this.props;
}
public
void
startElement(String
uri,
String
localName,
String
qName,
Attributes
attributes)
throws
SAXException
{
this.currentValue.delete(0,
this.currentValue.length());
this.currentName
=
qName;
}
public
void
characters(char[]
ch,
int
start,
int
length)
throws
SAXException
{
this.currentValue.append(ch,
start,
length);
}
public
void
endElement(String
uri,
String
localName,
String
qName)
throws
SAXException
{
this.props.put(qName.toLowerCase(),
this.currentValue.toString().trim());
}}xml文件
6
10
23:00
12:00
18:00jsp获取各个节点的值:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询