1

hi i've this little method:

private void modifyXML() {
        try {

            String filepath = main.fileWithPath;
            File f = new File(filepath);
            if(f.exists()){
                System.out.println("Exists");
            }



            DocumentBuilderFactory docFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(filepath);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("DAQChannel");

//          int sorszam = table.row_id-1;
//
//          Node nNode = nList.item(sorszam);
//          
//          if (nNode.getNodeType() == Node.ELEMENT_NODE) {
//              Element eElement = (Element) nNode;
//              
//              eElement.getElementsByTagName("Name").item(0).setTextContent(nameE.getText().toString());   
//              eElement.getElementsByTagName("Unit").item(0).setTextContent(nameE.getText().toString());
//              eElement.getElementsByTagName("Minimum").item(0).setTextContent(nameE.getText().toString());
//              eElement.getElementsByTagName("Maximum").item(0).setTextContent(nameE.getText().toString());
//              eElement.getElementsByTagName("Accuracy").item(0).setTextContent(nameE.getText().toString());
//              eElement.getElementsByTagName("SensorType").item(0).setTextContent(nameE.getText().toString());
//              eElement.getElementsByTagName("RegisterAddress").item(0).setTextContent(nameE.getText().toString());
//              eElement.getElementsByTagName("Offset").item(0).setTextContent(nameE.getText().toString());
//              eElement.getElementsByTagName("TimeStamp").item(0).setTextContent(nameE.getText().toString());
//          }

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);

            String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

            String ujPath = filepath.substring(0, filepath.length()-10); 

            StreamResult result = new StreamResult(new File(ujPath+"/config_midified_"+mydate+".xml"));
            transformer.transform(source, result);

            System.out.println("Done"); 

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SAXException sae) {
            sae.printStackTrace();
        }

    }

it prints out that the file exists right in the begining, but than i got the error message java.net.MalformedURLException: Protocol not found:

Please help me what did i wrong!

Thank you!

4

2 回答 2

3

而是传递一个File实例

Document doc = docBuilder.parse(new File(filepath));

传递 a 时StringDocumentBuilder#parse()需要它的URI格式

[scheme:][//authority][path][?query][#fragment]

即与协议一样file://

参考:
DocumentBuilder#parse(String)

于 2013-10-07T12:24:42.573 回答
0

异常很可能意味着您提供了需要 URL 的文件路径名。当 URL 解析器在 URL 中找不到协议组件时,会使用“Protocol not found”消息;即:URL 中第一个字符之前的字符。如果您要传递路径名,则路径中很可能没有 a :

于 2013-10-07T12:27:17.023 回答