Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java从xml中读取几个要点_Java_Xml_Dom - Fatal编程技术网

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

如何使用java从xml中读取几个要点,java,xml,dom,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>

我是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));
        }
    }

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

nList包含容器节点列表,而XML文档中只有一个这样的元素。您需要取而代之的是点元素:

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);
    ...

我的问题是它会循环一次

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

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));
}

代码中的问题是您正在使用nList.getLength而不是line.getLength终止循环

forint i=0;你指的是for循环吗?
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));
        }
    }
}