jszip icon indicating copy to clipboard operation
jszip copied to clipboard

Extracting xml file

Open africantearoa opened this issue 8 years ago • 4 comments

Hi,

I'm struggling to extract the xml content from a zip file. Wondered if anyone knew the best way to do so.

var new_zip = new JSZip();
// loading a zip file
JSZipUtils.getBinaryContent("testfile.zip", function (err, data) {
    if(err) {
        throw err; // or handle the error
    }
    new_zip.loadAsync(data)
        .then(function(zip) {
            // you now have every files contained in the loaded zip
            zip.forEach(function (relativePath, file) {
                if(relativePath.split(".")[1]==="xml"){
                    
                    //???


                }
            })
        });
});

africantearoa avatar Jan 19 '18 23:01 africantearoa

Think I've found it - was in the ZipEntry documentation but I couldn't understand what was going on with that and had to look elsewhere for an example that used it. FYI this is the current code. Needed to add a separate function to put the string data into an ActiveXObject to be able to parse it like an XML file.

var new_zip = new JSZip();
// loading a zip file
JSZipUtils.getBinaryContent("test.zip", function (err, data) {
    if(err) {
        throw err; // or handle the error
    }
    new_zip.loadAsync(data)
        .then(function(zip) {
            // you now have every files contained in the loaded zip
            zip.forEach(function (relativePath, file) {
                
                if(relativePath.split(".")[1]==="xml"){

                    file.async("string").then(function (data) {
                        //ParseXML is my function that parses the ActiveXObject
                        //stringToXML converts the string to an ActiveXObject
                        parseXML(stringToXML(data));

                    });


                }

            })
        });
});

function stringToXML(oString) {
    //code for IE
    if (window.ActiveXObject) {
        var oXML = new ActiveXObject("Microsoft.XMLDOM"); oXML.loadXML(oString);
        return oXML;
    }
    // code for Chrome, Safari, Firefox, Opera, etc.
    else {
        return (new DOMParser()).parseFromString(oString, "text/xml");
    }
}

Now to figure out how to extract images...

africantearoa avatar Jan 19 '18 23:01 africantearoa

hi~ I want to ask, is this function ( file.async("string").then... ) works on IE(11)? my code like yours just ran error [TypeError] on IE11.

Tinysusie avatar Apr 10 '19 10:04 Tinysusie

I think it should work on IE11

africantearoa avatar Apr 10 '19 20:04 africantearoa

I think it should work on IE11

ok,thankU

Tinysusie avatar Apr 11 '19 06:04 Tinysusie