Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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
使用jsp编码创建xml文件并存储在本地磁盘文件夹中_Jsp - Fatal编程技术网

使用jsp编码创建xml文件并存储在本地磁盘文件夹中

使用jsp编码创建xml文件并存储在本地磁盘文件夹中,jsp,Jsp,我可以使用jsp编码创建一个xml文件,并将其存储到一个本地磁盘中,该磁盘具有我们指定的名称。 xml文件结构如下所示 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <questions> <question category="something"/> </questions> 如果可能,请给出相应的编码。 <%@page import="java.io.*,org.w3

我可以使用jsp编码创建一个xml文件,并将其存储到一个本地磁盘中,该磁盘具有我们指定的名称。 xml文件结构如下所示

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<questions>
<question category="something"/>
</questions>

如果可能,请给出相应的编码。


<%@page import="java.io.*,org.w3c.dom.*,javax.xml.parsers.*,javax.xml.transform.*, javax.xml.transform.dom.*,javax.xml.transform.stream.*"%>  
<%!
public void createXml(String graph) throws Exception{
try{
    String str="dinesh";
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    // questionset elements
    Element rootElement = doc.createElement("questionset");
    doc.appendChild(rootElement);
    // question elements
    Element question = doc.createElement("question");
    rootElement.appendChild(question);
    // set attribute to question element
    Attr attr = doc.createAttribute("category");
    attr.setValue("graph");
    question.setAttributeNode(attr);
    // write the content into xml file        

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    String xmlString = sw.toString();

    File file=new File("C:xml/"+str+".xml");
    BufferedWriter bw = new BufferedWriter(new FileWriter(file));
    bw.write(xmlString);
    bw.flush();
    bw.close();
}
catch(Exception e)
{
System.out.println(e);
}   
}
%>
<% 
String graph=request.getParameter("graph");
createXml(graph);
%>
将上述编码另存为任何jsp页面,例如new.jsp

更改文件名str=“want u want” 还可以更改属性
attr.setValue(“want u want”)


您还可以更改要保存文件的本地磁盘文件夹

不了解如何将其存储在本地磁盘上。若您想从jsp输出xml,只需添加标题,然后放入xml