0

I'm trying to parse SVG file to get paths using Xpath within android application. Native java parse the path in following way.

try {
    Document document = builder.parse(
            new FileInputStream("c:\\employees.xml"));
} catch (SAXException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

I try to do using FileDescriptor as follows.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }


        AssetManager assetManager = getBaseContext().getAssets();
        AssetFileDescriptor assetFileDescriptor = null;
        try {
            assetFileDescriptor = assetManager.openFd("android.svg");

        } catch (IOException e) {
            e.printStackTrace();
        }
        FileDescriptor fileDescriptor = assetFileDescriptor.getFileDescriptor();
        FileInputStream stream = new FileInputStream(fileDescriptor);

        try {
            Document document = builder.parse(stream);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

However my app stopped working. What's wrong in my code?

isuru
  • 3,385
  • 4
  • 27
  • 62
  • 1
    I think the answer is already in stackoverflow. This may help you [link](http://stackoverflow.com/questions/14553292/fileinputstream-doesnt-work-with-the-relative-path) – vm345 Feb 22 '17 at 05:58

1 Answers1

0

You do not need FileDescriptor. Try as follows.

InputStream input = assetManager.open("android.svg"); //From your asset folder

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
    builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
    e.printStackTrace();
}

And parse your input to builder.

Document document = builder.parse(input);
Dis_Pro
  • 633
  • 1
  • 10
  • 21