Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JSF中上载和解析xml文件_Java_Xml_Jsf - Fatal编程技术网

Java 在JSF中上载和解析xml文件

Java 在JSF中上载和解析xml文件,java,xml,jsf,Java,Xml,Jsf,我想通过JSF Tomahawk上传一个xml文件,然后解析它 我创建了以下表单: <!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/t

我想通过JSF Tomahawk上传一个xml文件,然后解析它

我创建了以下表单:

<!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> 
我上传的文件是一个xml文件。 如何解析xml? 在我上面的代码中,上传文件后我得到了一个字节数组。
如何将其转换为xml文件并对其进行解析?

您不需要将其转换为xml文件。它已经是一个XML文件了。要解析它,只需使用JavaSE内置API或JavaEE内置API。如果您真的想将其保存在磁盘上,只需使用常规方法
FileOutputStream


这个问题与JSF上传文件无关。你成功地让那部分工作了。例如,当您通过
FileInputStream
获得
byte[]
时,您会遇到完全相同的问题。

您可以使用以下方法将该
byte[]
解析为
org.w3c.dom.Document

   InputStream is = new ByteArrayInputStream(byteArray);  
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
   DocumentBuilder builder = factory.newDocumentBuilder();  
   Document xml = builder.parse(is);
   InputStream is = new ByteArrayInputStream(byteArray);  
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
   DocumentBuilder builder = factory.newDocumentBuilder();  
   Document xml = builder.parse(is);