0

我需要解析存储在 blobstore 中的 XML 文件。我怎么做?

这是我到目前为止所做的:

FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.getBlobFile(new BlobKey(key));

我无法使用javax.xml包解析 XML 文件,或者可以吗?如果将该包中的类付诸实施,则需要引用java.io.File对象。但我没有那个。

这就是我到目前为止一直在做的事情:(不是在服务器上,而是在本地)

         File blobKeys = new File("/home/non-admin/NetBeansProjects/Personal Site_Testers/web/xml/xml_1.xml");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(blobKeys);
        Element root = doc.getDocumentElement();

        Element firstName = doc.createElement("first-name");
        firstName.setTextContent(name);

        root.appendChild(firstName);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("/home/non-admin/NetBeansProjects/Personal Site_Testers/web/xml/xml_1.xml"));
        transformer.transform(source, result);

注意:有 3 个 xml 文件上传到 blobstore。那些 xml 文件只有一个根标签。我想获取 xml 引用并解析它们以根据请求多次附加子节点。

4

1 回答 1

0

如果你想解析 *.xml,我认为jdom2可能更方便,我在应用程序引擎中使用它并且它可以工作。

但是,您可以阅读FileService API 文档,并且有一个如何读取和写入文件的示例。如果可以得到一个InputStream或者Reader,那么就可以使用javax.xml来解析InputStream或者Reader中的xml内容。

    FileService fileService = FileServiceFactory.getFileService();
    AppEngineFile readableFile = new AppEngineFile("your_file_name");
    FileReadChannel readChannel = fileService.openReadChannel(readableFile,
            false);
    InputStream input = Channels.newInputStream(readChannel);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(input);

    Element root = doc.getDocumentElement();

    Element firstName = doc.createElement("first-name");
    firstName.setTextContent("content");

    root.appendChild(firstName);
    // Now write
    GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
            .setBucket("mybucket").setKey("myfile")
            .setMimeType("text/html").setAcl("public_read")
            .addUserMetadata("myfield1", "my field value"); // change as
                                                            // your need
    AppEngineFile writableFile = fileService.createNewGSFile(optionsBuilder
            .build());
    FileWriteChannel writeChannel = fileService.openWriteChannel(
            writableFile, false);
    OutputStream output = Channels.newOutputStream(writeChannel);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(doc);
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(output);
    transformer.transform(source, result);
于 2012-08-30T13:54:22.720 回答