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!

3
  • 1
    what is the value of filepath?! Commented Oct 7, 2013 at 12:20
  • Please include the full stack trace of your error. Commented Oct 7, 2013 at 12:21
  • 10-07 14:16:27.398: W/System.err(25753): java.net.MalformedURLException: Protocol not found: /storage/emulated/0/Ementor/config.xml Commented Oct 7, 2013 at 12:22

2 Answers 2

4

Pass a File instance instead

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

When passing a String, DocumentBuilder#parse() needs it to be in the URI format

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

i.e. along with the protocol like file://.

Reference :
DocumentBuilder#parse(String)

Sign up to request clarification or add additional context in comments.

Comments

0

The exception most likely means that you are providing a file pathname where a URL is required. The message "Protocol not found" is used when the URL parser cannot find a protocol component in a URL; i.e. the characters before the first : in the URL. If you are passing a pathname, there most likely isn't a : in the path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.