急!java读取xml文件
<?xmlversion="1.0"encoding="GB2312"?><Database><User><Itemid="1"name=""/><Itemid="2"n...
<?xml version="1.0" encoding="GB2312"?>
<Database>
<User>
<Item id="1" name="" />
<Item id="2" name="" />
<Item id="3" name="" />
</User>
<Dept>
<Item id="1" name="" />
<Item id="2" name="" />
<Item id="3" name="" />
</Dept>
</Database>
如何循环遍历来获取user及dept下item中的id和name属性值 展开
<Database>
<User>
<Item id="1" name="" />
<Item id="2" name="" />
<Item id="3" name="" />
</User>
<Dept>
<Item id="1" name="" />
<Item id="2" name="" />
<Item id="3" name="" />
</Dept>
</Database>
如何循环遍历来获取user及dept下item中的id和name属性值 展开
4个回答
展开全部
package testAll.t20150904;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
String xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?><Database><User><Item id=\"1\" name=\"\" /><Item id=\"2\" name=\"\" /><Item id=\"3\" name=\"\" /></User><Dept><Item id=\"1\" name=\"\" /><Item id=\"2\" name=\"\" /><Item id=\"3\" name=\"\" /></Dept></Database>";
ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(bais);
Element Database = (Element) doc.getElementsByTagName("Database").item(0);
Element User = (Element) Database.getElementsByTagName("User").item(0);
Element Dept = (Element) Database.getElementsByTagName("Dept").item(0);
NodeList users = User.getChildNodes();
System.out.println("User:");
for(int i=0;i<users.getLength();i++){
Element user = (Element) users.item(i);
System.out.println("id="+user.getAttribute("id")+",name="+user.getAttribute("name"));
}
NodeList depts = Dept.getChildNodes();
System.out.println("Dept:");
for(int i=0;i<depts.getLength();i++){
Element dept = (Element) depts.item(i);
System.out.println("id="+dept.getAttribute("id")+",name="+dept.getAttribute("name"));
}
}
}
2015-10-09
展开全部
DOM 就是不断枚举 nodes
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用dom4j解析
public class test {
public static void main(String [] args){
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(new FileInputStream("D:/lineStructure.xml"));
Element node = doc.getRootElement();
//遍历所有的元素节点
listNodes(node);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 遍历当前节点元素下面的所有(元素的)子节点
*
* @param node
*/
public static void listNodes(Element node) {
System.out.println("当前节点的名称::" + node.getName());
// 获取当前节点的所有属性节点
List<Attribute> list = node.attributes();
// 遍历属性节点
for (Attribute attr : list) {
System.out.println(attr.getText() + "-----" + attr.getName()
+ "---" + attr.getValue());
}
if (!(node.getTextTrim().equals(""))) {
System.out.println("文本内容::::" + node.getText());
}
// 当前节点下面子节点迭代器
Iterator<Element> it = node.elementIterator();
// 遍历
while (it.hasNext()) {
// 获取某个子节点对象
Element e = it.next();
// 对子节点进行遍历
listNodes(e);
}
}
}
public class test {
public static void main(String [] args){
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(new FileInputStream("D:/lineStructure.xml"));
Element node = doc.getRootElement();
//遍历所有的元素节点
listNodes(node);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 遍历当前节点元素下面的所有(元素的)子节点
*
* @param node
*/
public static void listNodes(Element node) {
System.out.println("当前节点的名称::" + node.getName());
// 获取当前节点的所有属性节点
List<Attribute> list = node.attributes();
// 遍历属性节点
for (Attribute attr : list) {
System.out.println(attr.getText() + "-----" + attr.getName()
+ "---" + attr.getValue());
}
if (!(node.getTextTrim().equals(""))) {
System.out.println("文本内容::::" + node.getText());
}
// 当前节点下面子节点迭代器
Iterator<Element> it = node.elementIterator();
// 遍历
while (it.hasNext()) {
// 获取某个子节点对象
Element e = it.next();
// 对子节点进行遍历
listNodes(e);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class TestUtil {
private static Logger logger = Logger.getLogger(TestUtil.class);
/***
* @describe 在classpath下解析xml配置文件
* @param filePath
* @return
* @throws Exception
*/
public static Document parseXmlFile(String fileName) throws Exception{
Document document = null;
try{
SAXReader reader = new SAXReader();
InputStream is = ParseXmlUtil.class.getResourceAsStream(fileName);
document = reader.read(is);
}catch(Exception e){
logger.error("读取CLASSPATH下的接口查询配置文件" + fileName + "发生异常,请检查文件是否存在!");
logger.error(e);
throw e;
}
return document;
}
public static void parseValue(String fileName) throws Exception{
Document document = parseXmlFile(fileName);
Element root = document.getRootElement();
Element userElement = root.element("User");
Element deptElement = root.element("Dept");
Iterator userIterator = userElement.elementIterator();
Iterator deptIterator = deptElement.elementIterator();
while(userIterator.hasNext()){
Element one = (Element) userIterator.next();
String idValue = one.attributeValue("id");
String nameValue = one.attributeValue("name");
System.out.println(idValue + "," + nameValue);
}
while(deptIterator.hasNext()){
Element one = (Element) deptIterator.next();
String idValue = one.attributeValue("id");
String nameValue = one.attributeValue("name");
System.out.println(idValue + "," + nameValue);
}
}
public static void main(String[] args) throws Exception{
String fileName = "/testXml.xml";
parseValue(fileName);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询