XML解析错误(Java)

时间:2017-01-24 05:12:13

标签: java xml xml-parsing

您好我在点击按钮时遇到异常错误。我无法得到解决方案。 这是我的代码: -

的java: -

JButton btnx1y1 = new JButton(">");
        btnx1y1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    String filePath = "C:\\Users\\Admin\\workspace\\generator\\data\\f.xml";
                    File inputFile = new File(filePath);
                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();  
                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                    Document document = dBuilder.parse(inputFile);

                    Node control = document.getElementsByTagName("control").item(0); 

                    NamedNodeMap attr = control.getAttributes(); 
                    Node nodeAttr = attr.getNamedItem("xpos"); 
                    nodeAttr.setTextContent(str201x);
                    attr.getNamedItem("ypos").setTextContent(str201y);

                    NodeList list = control.getChildNodes();


                    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
                    Transformer transformer= transformerFactory.newTransformer(); 
                    DOMSource source = new DOMSource(document); 
                    StreamResult result=new StreamResult(new File(filePath)); 
                    transformer.transform(source, result);

                    StreamResult consoleResult = new StreamResult(System.out); 
                    transformer.transform(source, consoleResult); 

            }catch (Exception e1){
                e1.printStackTrace();
            }

            }
        });

从文本字段中挑选值: - 对于x坐标

textR201x = new JTextField();
String str201x=textR201x.getText();
str201x="";

表示y坐标

textR201y = new JTextField();
String str201y=textR201y.getText();
str201y="";

要更新xpos和ypos值的XML节点: -

<control name="control_200" xpos="233" ypos="25" width="60" type="float" format="%7.2f" refvar="gud/_ZSFR[29]" hotlink="true">

在此显示的错误是 - 在封闭范围内定义的局部变量str200x必须是最终的或有效的最终。我在解决这个问题时遇到了麻烦。

0 个答案:

没有答案