import
org.jdom.Document;
import
org.jdom.Element;
import
org.jdom.input.SAXBuilder;
import
java.io.
*
;
import
java.util.
*
;
public
class
Test
...
{
public
Test()
...
{
}
public
static
void
main(String[]args)
...
{
Documentdom
=
null
;
try
...
{
SAXBuilderbuild
=
new
SAXBuilder();
//
dom=build.build(newFileInputStream("test.xml"));
dom
=
build.build(Test.
class
.getResource(
"
test.xml
"
));
}
catch
(Exceptionex)
...
{
ex.printStackTrace();
}
Elementele
=
dom.getRootElement();
System.out.println(ele.getAttributeValue(
"
name
"
));
Listlist
=
ele.getChildren();
Iteratorit
=
list.iterator();
while
(it.hasNext())
...
{
Elementel
=
(Element)it.next();
System.out.println(el.getAttributeValue(
"
name
"
));
System.out.println(el.getText());
}
}
}
你先要导入 jdom包
test.xml
<?
xmlversion="1.0"encoding="gb2312"
?>
<
province
name
="liu"
>
<
city
name
="xiang"
>
东城
</
city
>
<
city
>
西城
</
city
>
<
city
>
崇文
</
city
>
</
province
>