解析类:
package tt;import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;public class TestURL { /**
* @param args
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
static ArrayList list = new ArrayList();
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
// TODO Auto-generated method stub
String abc="abc.xml";
URL url = TestURL.class.getClassLoader().getResource(abc);
String filePath = url.getPath();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(filePath));
Element element = doc.getDocumentElement();
NodeList nodeList = element.getChildNodes();
System.out.println(getAllSubNodesName(nodeList));
}
public static List getAllSubNodesName(NodeList nodeList){
for(int i=0;i<nodeList.getLength();i++){
Node node = nodeList.item(i);
if(node.getChildNodes().getLength()<1){
System.out.println(node.getNodeValue());
list.add(node.getNodeValue());
}
getAllSubNodesName(node.getChildNodes());
}
System.out.println(list.size());
return list;
}
}xml格式:
<?xml version="1.0" encoding="UTF-8"?>
<all><book><title>the mythical man-month</title><writer>frederick p.brooks Jr.</writer><publishdate>1975-03-12</publishdate></book><book><title>the mythical man-month</title><writer>frederick p.brooks Jr.</writer><publishdate/></book></all>
输出结果:
the mythical man-month
1
1
frederick p.brooks Jr.
2
2
1975-03-12
3
3
3the mythical man-month
4
4
frederick p.brooks Jr.
5
5
null
6
6
6
[the mythical man-month, frederick p.brooks Jr., 1975-03-12, the mythical man-month, frederick p.brooks Jr., null]
为什么输入数字那么多次?为什么不应该是一次6.
package tt;import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;public class TestURL { /**
* @param args
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
static ArrayList list = new ArrayList();
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
// TODO Auto-generated method stub
String abc="abc.xml";
URL url = TestURL.class.getClassLoader().getResource(abc);
String filePath = url.getPath();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(filePath));
Element element = doc.getDocumentElement();
NodeList nodeList = element.getChildNodes();
System.out.println(getAllSubNodesName(nodeList));
}
public static List getAllSubNodesName(NodeList nodeList){
for(int i=0;i<nodeList.getLength();i++){
Node node = nodeList.item(i);
if(node.getChildNodes().getLength()<1){
System.out.println(node.getNodeValue());
list.add(node.getNodeValue());
}
getAllSubNodesName(node.getChildNodes());
}
System.out.println(list.size());
return list;
}
}xml格式:
<?xml version="1.0" encoding="UTF-8"?>
<all><book><title>the mythical man-month</title><writer>frederick p.brooks Jr.</writer><publishdate>1975-03-12</publishdate></book><book><title>the mythical man-month</title><writer>frederick p.brooks Jr.</writer><publishdate/></book></all>
输出结果:
the mythical man-month
1
1
frederick p.brooks Jr.
2
2
1975-03-12
3
3
3the mythical man-month
4
4
frederick p.brooks Jr.
5
5
null
6
6
6
[the mythical man-month, frederick p.brooks Jr., 1975-03-12, the mythical man-month, frederick p.brooks Jr., null]
为什么输入数字那么多次?为什么不应该是一次6.
解决方案 »
- jtable怎么跨窗口刷新数据显示呢?有截图 求大家帮帮
- 类方法中调用实例方法?
- 将1,2,3,4,5的各种排列顺序打印出来?如:12345,12354。应该有5!=120种吧(最好用递归)
- 数字计算字节数??
- 听说《core java》流行两个中文版本,第六版和第七版,我注意到译者不同,不知道哪本更好?
- oracle的一个问题,求证
- 简单的字符串比较问题,大家快来帮忙或学习呀!
- java子类的字段要加注解,怎么继承?
- 懂得snmp网管编程的请进,如何设置类型为MacAddress的值
- 求java大佬解释
- org.apache.xerces.parsers.DOMParser 问题
- if else switch 的问题
Node node = nodeList.item(i);
if(node.getChildNodes().getLength() <1){
System.out.println(node.getNodeValue()); //这里循环一次,把这里去掉
list.add(node.getNodeValue());
}
getAllSubNodesName(node.getChildNodes());
}
System.out.println(list.size()); //这里循环一次
return list; }
}
这行语句是输出Node的value的。既
the mythical man-month
frederick p.brooks Jr.
1975-03-12
the mythical man-month
frederick p.brooks Jr.
null
在你循环体里不是么。
把两个System.out.println(...); 删除掉,
在main方法中的getAllSubNodesName(nodeList)就是最后的list(含有6个元素的list结构)