1

我正在开发一个 Java 应用程序,我必须映射大量数据然后生成 XML 输出。

所有的输出看起来都不错,除了这 1 个随机元素。正如您在下图中看到的,所有“值”标签看起来都不错,除了突出显示的 1。

http://i.stack.imgur.com/3TgIG.png 随机缺失的开口

用于生成 XML 的代码:

private void generateXML() {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        doc = docBuilder.newDocument();

        //Add Root Element
        Element rootElement = doc.createElement("Request");
        rootElement.setAttribute("request_id", requestID);
        doc.appendChild(rootElement);

        //Add Experiment Element
        Element experimentElement = doc.createElement("Experiment");
        experimentElement.setAttribute("name", mappedData.getFileName()); //Change to experiment name
        experimentElement.setAttribute("insert_method", "Insert");
        experimentElement.setAttribute("notebook", "BioELN");
        experimentElement.setAttribute("researcher", webFormPostData.get("Researchers")[0]);
        experimentElement.setAttribute("project", getExcelTypeData.get("project"));
        experimentElement.setAttribute("comments", webFormPostData.get("comments")[0]);
        experimentElement.setAttribute("expt_date", getCurrentDate());
        experimentElement.setAttribute("protocol_name", getExcelTypeData.get("protocolName"));
        experimentElement.setAttribute("protocol_version", getExcelTypeData.get("protocolVersion"));
        rootElement.appendChild(experimentElement);

        //Add Data to Experiment
        List<Element> experimentDataElements = generateExperimentDataElements();
        for(Element dataElement : experimentDataElements) {
            experimentElement.appendChild(dataElement);
        }

        Element attachmentElement = doc.createElement("Attachment");
        attachmentElement.setAttribute("filename", mappedData.getFilePath());

        experimentElement.appendChild(attachmentElement);
    } catch(Exception e) {
        logger.log("Error: " + e);
        e.printStackTrace();
    }
}
private List<Element> generateExperimentDataElements() {
    //Loop through output_mapping mapped data and see if values are found in the mappedData.getMappedData();        
    List<Element> dataElements = new ArrayList<Element>();

    for(int i=0; i < mappedData.getMappedData().size(); i++) { //Cycle Through all Mapped Data Rows
        for(Map.Entry<String, Map<String, String>> entry : outputMappedFields.entrySet()) { //Get Required Fields
            Element itemElement, valueElement, dataElement;

            String mappedVarName = entry.getKey();
            String vName = entry.getValue().get("variableName");
            String variableType = entry.getValue().get("variableType");

            itemElement = doc.createElement("VGItem");
            itemElement.setAttribute("row_id", String.valueOf(i+1));
            itemElement.setAttribute("table_name", "Results");
            itemElement.setAttribute("variable_name", vName);
            itemElement.setAttribute("parent_table_name", "Plate");
            itemElement.setAttribute("parent_row_id", "plate");

            valueElement = doc.createElement("Value");
            dataElement = doc.createElement(variableType+"_value");
            if(mappedData.getMappedData().get(i).containsKey(mappedVarName)) {
                dataElement.setTextContent(mappedData.getMappedData().get(i).get(mappedVarName)); //Get field from Mapped Data
            }

            valueElement.appendChild(dataElement);
            itemElement.appendChild(valueElement);
            dataElements.add(itemElement);
        }
    }

    return dataElements;
}
@Override
public String toString() {
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + writer.getBuffer().toString().replaceAll("\n|\r", "");

        return output;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

XML 文件: http: //paste.strictfp.com/38339

4

0 回答 0