How to read XML file using FileReader javascript?

前端 未结 2 1474
不知归路
不知归路 2021-01-21 01:56

I need to get XML from a ODF file. I tried using FileReader readAsText and readAsBinaryString but its not working.

FileReader readAsText returns some s

相关标签:
2条回答
  • 2021-01-21 02:04

    Here's a browser-based example, but this should be applicable to other JavaScript contexts:

    Make a Form:

    <div id="upload">
        <h2>Gimme Yo ODF Cornbread</h2>
        <form enctype="multipart/form-data" method="post">
            <input type="file" name="odfxml" id="odfxml" />
        </form>
    </div>
    

    Handle the Upload: (I'm using JQuery for brevity/simplicity)

    <script>
        $("#odfxml").change(function(){
            var file = document.getElementById("odfxml").files[0];
                        //You could insert a check here to ensure proper file type
            var reader = new FileReader();
            reader.readAsText(file);
            reader.onloadend = function(){
                var xmlData = $(reader.result);
            };
        });
    </script>
    

    The xmldata variable contains your XML, ready for your magic.

    0 讨论(0)
  • 2021-01-21 02:08

    Using the response like text:

    var xml = data.replace(/[\s\S]+<\?xml/, '<?xml');
    xml = xml.replace(/office:document\-meta>[\s\S]+/, 'office:document-meta>');
    

    :)

    If you need to load the xml like jquery object:

    xml = $(xml);
    

    then you can use jquery selector's on

    0 讨论(0)
提交回复
热议问题