如何使用java从xml中读取几个点

时间:2015-01-22 17:09:17

标签: java xml dom

我是XML新手,我想将XML文件中的点附加到我写的点容器中。

这是XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<container>
    <point>
        <X>56</X>
        <Y>58</Y>
    </point>
    <point>
        <X>59</X>
        <Y>40</Y>
    </point>
    <point>
        <X>70</X>
        <Y>30</Y>
    </point>
</container>

这就是我所做的:

private void OpenFile () throws ParserConfigurationException, SAXException,       IOException {
    JFileChooser of = new JFileChooser();
    int option = of.showOpenDialog(of);
    while (!of.getSelectedFile().getName().endsWith(".xml")) {
        String error = "Error, Please select txt file";
        JOptionPane.showMessageDialog(this, error, "Wrong type of file", JOptionPane.INFORMATION_MESSAGE);
        of = new JFileChooser();
        option = of.showOpenDialog(of);
    }
    if (option == JFileChooser.APPROVE_OPTION){
        thisFile =  new File(of.getSelectedFile().getPath());
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(thisFile);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("container");
        Element line = (Element) nList.item(0);
        for(int i =0 ; i < nList.getLength() ; i++) {
            Element point = (Element) line.getElementsByTagName("point").item(i);
            x = Integer.parseInt(point.getElementsByTagName("X").item(0).getTextContent());
            y = Integer.parseInt(point.getElementsByTagName("Y").item(0).getTextContent());
            drewPoints(x, y);
            pc.add(new Point(x, y));
        }
    }

我的问题是它循环了一次。

3 个答案:

答案 0 :(得分:2)

nList包含container个节点的列表,并且XML文档中只有一个这样的元素。您需要获取point元素:

NodeList nList = doc.getElementsByTagName("container");
Element containerElement = (Element) nList.item(0);
NodeList pointNodes = containerElement.getElementsByTagName("point");
for(int i = 0; i < pointNodes.getLength(); i++) {
    Element point = (Element) pointNodes..item(i);
    ...

答案 1 :(得分:1)

“我的问题是它循环了一次。”

- 这是因为它们只是一个<container>节点正在迭代:

NodeList nList = doc.getElementsByTagName("container"); // nList.getLength() == 1 here
Element line = (Element) nList.item(0);
for(int i =0 ; i < nList.getLength() ; i++) {  // looping from i = 0 to i = 1

要使其迭代所有点,请执行以下操作:

NodeList nList = doc.getElementsByTagName("container");
Element container = (Element) nList.item(0);
NodeList pointsList = container.getElementsByTagName("point");
for (int i = 0; i < pointsList.getLength(); i++) {
    Element point = (Element) pointsList.item(i);
    x = Integer.parseInt(point.getElementsByTagName("X").item(0).getTextContent());
    y = Integer.parseInt(point.getElementsByTagName("Y").item(0).getTextContent());
    drewPoints(x, y);
    pc.add(new Point(x, y));
}

答案 2 :(得分:1)

您的代码中的问题是您使用nList.getLength()代替line.getLength()来终止循环。

for(int i =0 ; i < line .getLength() ; i++) {

您的更新代码,这应该可以正常工作:

private void OpenFile () throws ParserConfigurationException, SAXException,       IOException {
    JFileChooser of = new JFileChooser();
    int option = of.showOpenDialog(of);
    while (!of.getSelectedFile().getName().endsWith(".xml")) {
        String error = "Error, Please select txt file";
        JOptionPane.showMessageDialog(this, error, "Wrong type of file", JOptionPane.INFORMATION_MESSAGE);
        of = new JFileChooser();
        option = of.showOpenDialog(of);
    }
    if (option == JFileChooser.APPROVE_OPTION){
        thisFile =  new File(of.getSelectedFile().getPath());
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(thisFile);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("container");
        Element line = (Element) nList.item(0);
        for(int i =0 ; i < line.getLength() ; i++) {
            Element point = (Element) line.getElementsByTagName("point").item(i);
            x = Integer.parseInt(point.getElementsByTagName("X").item(0).getTextContent());
            y = Integer.parseInt(point.getElementsByTagName("Y").item(0).getTextContent());
            drewPoints(x, y);
            pc.add(new Point(x, y));
        }
    }
}