0

嘿,由于我无法掌握我所缺少的内容,我对下面的代码有疑问。我知道问题是每次调用searchXML()它只是读取没有编辑的原始文件。但是我不知道如何使用原件然后不使用原件,因为我在下面的尝试似乎不起作用....

static int X                        = 0;
public static void main(String[] args) {
    String[] tags = {"<!--D01-->:2000-12-08",
                     "<!--D02-->:2010-01-02",
                     "<!--D03-->:1990-02-29",
                     "<!--D04-->:2015-09-12",
                     "<!--D05-->:1996-04-04",
                     "<!--D06-->:2002-05-22"};

    try {
        for (int i = 0; i < tags.length; i++) {
            searchXML(tags[i], tags.length);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void searchXML(String TAG, int MaxNumber) throws Exception {
    Scanner input                           = null;
    String modifiedXML                      = "";
    String theText                          = "";
    String currentLineValue                 = null;

    // Split array into 2 separate values [0]xxxxx [1]zzzzz
    String[] _tmpSplit                      = TAG.split(":");

    if (i >= 1) {
        //We've been around 1 or more times (load modified xml file)
        input = new Scanner(new File("c:/temp/newXML.xml"));
    } else {
        //We've been around only 1 time (load original xml file)
        input = new Scanner(new File("c:/temp/anotherxml.xml"));
    }

    while (input.hasNextLine()) {
        currentLineValue                    = input.nextLine().trim();
        currentLineValue                    = currentLineValue.replace("<!-- ", "<!--").replace(" -->", "-->");

        if (currentLineValue.contains(_tmpSplit[0])) {
            modifiedXML                     += currentLineValue + "\n";
            currentLineValue                = input.nextLine();

            //check if this is just a blank tag
            int blankTag                    = currentLineValue.indexOf("</");

            if (blankTag == -1) {
                //Its blank so go to the next line
                modifiedXML                 += currentLineValue + "\n";
                //Now grab that new lines text (that we wanted originally)
                currentLineValue            = input.nextLine();
            }

            //Check for the position number of where the > ends
            theText                         = currentLineValue
                                                .substring(currentLineValue
                                                    .indexOf(">"), 
                                              currentLineValue
                                                .indexOf("</"))
                                                    .replace(">", "")
                                                        .trim();

            //Now replace the old value (_tmpSplit[1]) with the new value (theText).
            currentLineValue                = currentLineValue.replace(theText, _tmpSplit[1]);
        }

        //Finally save the modified value (if...) or original value (else...)
        modifiedXML                         += currentLineValue;
    }

    input.close();
    X++;

    //Format and save new XML to file if we reached X
    if (MaxNumber == X) {
        Scanner input                       = new StreamSource(new StringReader(modifiedXML));
        StringWriter stringWriter           = new StringWriter();
        String pretty                       = null;

        try {
            // Format the 1 row string XML we made into pretty line(s)/tab(s) xml
            Transformer transformer         = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.transform(xmlInput, new StreamResult(stringWriter));
            pretty                          = stringWriter.toString();
            pretty                          = pretty.replace("\r\n", "\n");                         
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // Write the new xml file
        BufferedWriter writer               = new BufferedWriter(new FileWriter("c:/temp/newXML.xml"));
        writer.write(pretty);
        writer.flush();
        writer.close();         
    }
}

anotherxml.xml = 需要更改的主要 xml 文件。

newXML.xml = 具有更改的新 xml 文件。

如果这里有人可以解决这个问题并让我知道我需要做什么才能做到这一点。

能够读取旧的xml。

遍历它以找到 X 个可替换值。

使用该值保存新的 XML。

转到我们正在寻找的下一个值。

这一次它会加载修改后的 xml。

重复。

4

0 回答 0