简体   繁体   English

如何在Java中将多个xml文件合并为单个字符串

[英]How to combine multiple xml files into a single string in java

I have several xml files with different node structure. 我有几个具有不同节点结构的xml文件。 I want to extract xml content from each of these xml files and store former(xml content) as a single string. 我想从每个这些xml文件中提取xml内容,并将前一个(xml内容)存储为单个字符串。

I am using following code to achieve it 我正在使用以下代码来实现它

   String xmlContent = FileUtils.readFileToString(new File("xyz"), "UTF-8");

but i am facing following error 但我面临以下错误

    [Fatal Error] :5616:15: The processing instruction target matching "[xX][mM][lL]" is not allowed.

After searching i found that aforesaid error occurs because "" present in between the xml content. 搜索后,我发现上述错误发生是因为xml内容之间存在“”。

Should i first modify xml files or there is any other way handle this situation? 我应该首先修改xml文件还是有其他方法来处理这种情况?

Edit your xml file using a text editor. 使用文本编辑器编辑xml文件。 At the very beginning of the first line, look for and delete any leading spaces. 在第一行的开头,查找并删除所有前导空格。

The following first line of the xml must not have any spaces or characters before the first "<" in the line. xml的以下第一行在该行的第一个“ <”之前不得有任何空格或字符。

<?xml version="1.0" encoding="UTF-8"?>

Try the import again. 再次尝试导入。 refer http://www-01.ibm.com/support/docview.wss?uid=swg21262072 请参阅http://www-01.ibm.com/support/docview.wss?uid=swg21262072

Should i first modify xml files ... 我应该首先修改xml文件吗?

Yes you should. 是的你应该。

... and there is any other way handle this situation? ...还有其他方法可以处理这种情况吗?

Well, you could attempt to program around the fact that your XML is invalid: 好吧,您可以尝试围绕XML无效这一事实进行编程:

  • If the problems are minor and isolated, you could attempt to do an initial pass over the file to "fix" it. 如果问题较小且不严重,则可以尝试对文件进行初始传递以“修复”该文件。 Then parse it using a regular XML parser. 然后使用常规XML解析器对其进行解析。

  • If the problems are too widespread, you could attempt to treat the XML as text and attempt to extract data without parsing properly. 如果问题过于普遍,则可以尝试将XML视为文本,并尝试在不正确分析的情况下提取数据。

But the problem with both approaches is that you could end up with bad or missing data when your attempts to make good fail; 但是,这两种方法的问题都是,当您尝试使成功变为失败时,最终可能会导致数据丢失或丢失。 eg because the XML changes to be bad in a different way. 例如,因为XML以不同的方式变为不良。

So my recommendation would be to either fix the XML by hand using a text editor (if this is a once) off, or get who / whatever is creating it to fix the problem at source. 因此,我的建议是关闭使用文本编辑器(如果是一次性的话)手动修复XML的方法,或者由谁/无论通过何种方式创建XML来从源头解决问题。


Incidentally, once you have fixed the XML syntax problems, you may be able to do the picking and merging of the XML files just using XSLT. 顺便说一句,一旦解决了XML语法问题,就可以仅使用XSLT来进行XML文件的选择和合并。

After putting some efforts I have combined content into a single xml file :) Following method removes xml encoding node and stringifies xml file. 经过一些努力,我已将内容组合到单个xml文件中:)以下方法删除了xml编码节点并对xml文件进行了字符串化。

 public String stringifyXmlFiles(String fileName) {
        File file = new File(fileName);
    String content = "";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new FileInputStream(file));

        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
        aTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                "yes");
        aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
        Source src = new DOMSource(document);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Result dest = new StreamResult(stream);
        aTransformer.transform(src, dest);
        content = stream.toString();
    } catch (ParserConfigurationException e) {
        logger.error(e.getMessage(), e);
    } catch (SAXException e) {
        logger.error(e.getMessage(), e);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } catch (TransformerConfigurationException e) {
        logger.error(e.getMessage(), e);
    } catch (TransformerException e) {
        logger.error(e.getMessage(), e);
    }
    return content;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM