I want to upload an xml file via JSF Tomahawk and then parse it.
I have created the following form:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:t="http://myfaces.apache.org/tomahawk"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Import</title>
</h:head>
<h:body>
<h1>Import</h1>
<h:form enctype="multipart/form-data">
XML Datei mit den Kursdaten:
<t:inputFileUpload value="#{installationBean.uploadedFile}" />
<h:commandButton value="submit" action="#{installationBean.submit}" />
<h:messages />
</h:form>
</h:body>
This is the bean:
package installation;
import java.io.IOException;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.apache.commons.io.FilenameUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;
@Stateless
@LocalBean
@ManagedBean
@ViewScoped
public class InstallationBean {
private UploadedFile uploadedFile;
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile value) {
uploadedFile = value;
}
public void submit()throws IOException {
String fileName = FilenameUtils.getName(uploadedFile.getName());
String contentType = uploadedFile.getContentType();
byte[] bytes = uploadedFile.getBytes();
// Parse xml
}
}
The file that i upload is an xml file.
How can I parse the xml?
In my code above I have got a byte array after uploading the file.
How can I convert this to an xml file and parse it?
You could use the following to parse this
byte[]into aorg.w3c.dom.Document.