Java 从文件夹读取并更新XML标记值

Java 从文件夹读取并更新XML标记值,java,xpath,Java,Xpath,我有一个文件夹(“容器”),里面有一些.txt文件,实际上包含XML。所有文件都有一个公共元素,对于所有XML文件,我希望该元素增加1。 任何帮助 我已经对单个.txt文件进行了更改,这是可以的:- package ParsingXML; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilde

我有一个文件夹(“容器”),里面有一些.txt文件,实际上包含XML。所有文件都有一个公共元素
,对于所有XML文件,我希望该元素增加1。 任何帮助

我已经对单个.txt文件进行了更改,这是可以的:-

package ParsingXML;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class ReadAndModifyXMLFile {
    public static final String xmlFilePath = "C:\\Users\\PSINGH17\\Desktop\\testFile.txt";

    public static void main(String argv[]) {

    try {

            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

            Document document = documentBuilder.parse(xmlFilePath);

            // Get employee by tag name
            //use item(0) to get the first node with tag name "employee"
            Node employee = document.getElementsByTagName("employee").item(0);

            // update employee , increment the number
            NamedNodeMap attribute = employee.getAttributes();
            Node nodeAttr = attribute.getNamedItem("number");
String str= nodeAttr.getNodeValue();
System.out.println(str);
int val= Integer.parseInt(str);
System.out.println(val);
val=val+1;
System.out.println(val);
String final_value= String.valueOf(val);
System.out.println(final_value);
            nodeAttr.setTextContent(final_value);

            TransformerFactory transformerFactory = TransformerFactory.newInstance();

            Transformer transformer = transformerFactory.newTransformer();
            DOMSource domSource = new DOMSource(document);

            StreamResult streamResult = new StreamResult(new File(xmlFilePath));
            transformer.transform(domSource, streamResult);



        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SAXException sae) {
            sae.printStackTrace();
        }
    }
}

xml文件:-

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


<employee number="14">

    <firstname>Pranav</firstname>

    <lastname>Singh</lastname>
    <email>pranav@example.org</email>


    <salary>1000</salary>

</employee>


</company>

普拉纳夫
辛格
pranav@example.org
1000

因此此代码使用散列后的初始值数字来启动计数器。 然后遍历文件夹中的所有文件。 第一个文件将获得初始值,其余文件将获得递增值

    import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

public class TestFile {
    public static final String xmlFileFolder = "C:\\Rahul\\test";
    private static final String initialValue = "450-000-1212";

    public static void main(String argv[]) {
        int baseValue = Integer.parseInt(getValueAfterLastDash(initialValue));
        System.out.println("startValue " + baseValue);
        File folder = new File(xmlFileFolder);
        for (File file : folder.listFiles()) {
            try {
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

                Document document = documentBuilder.parse(file);

                Node employee = document.getElementsByTagName("employee").item(0);
                NamedNodeMap attribute = employee.getAttributes();
                Node nodeAttr = attribute.getNamedItem("number");
                System.out.println(getValueBeforeLastDash(initialValue) + baseValue);
                nodeAttr.setTextContent(getValueBeforeLastDash(initialValue) + baseValue);

                TransformerFactory transformerFactory = TransformerFactory.newInstance();

                Transformer transformer = transformerFactory.newTransformer();
                DOMSource domSource = new DOMSource(document);

                StreamResult streamResult = new StreamResult(file);
                transformer.transform(domSource, streamResult);
                baseValue++;
            } catch (Exception ignored) {
            }
        }
    }

    private static String getValueAfterLastDash(String initialValue) {
        return initialValue.substring(initialValue.lastIndexOf('-') + 1, initialValue.length());
    }

    private static String getValueBeforeLastDash(String initialValue) {
        return initialValue.substring(0, initialValue.lastIndexOf('-') + 1);
    }
}

因此,这段代码使用散列后的初始值数字来启动计数器。 然后遍历文件夹中的所有文件。 第一个文件将获得初始值,其余文件将获得递增值

    import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

public class TestFile {
    public static final String xmlFileFolder = "C:\\Rahul\\test";
    private static final String initialValue = "450-000-1212";

    public static void main(String argv[]) {
        int baseValue = Integer.parseInt(getValueAfterLastDash(initialValue));
        System.out.println("startValue " + baseValue);
        File folder = new File(xmlFileFolder);
        for (File file : folder.listFiles()) {
            try {
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

                Document document = documentBuilder.parse(file);

                Node employee = document.getElementsByTagName("employee").item(0);
                NamedNodeMap attribute = employee.getAttributes();
                Node nodeAttr = attribute.getNamedItem("number");
                System.out.println(getValueBeforeLastDash(initialValue) + baseValue);
                nodeAttr.setTextContent(getValueBeforeLastDash(initialValue) + baseValue);

                TransformerFactory transformerFactory = TransformerFactory.newInstance();

                Transformer transformer = transformerFactory.newTransformer();
                DOMSource domSource = new DOMSource(document);

                StreamResult streamResult = new StreamResult(file);
                transformer.transform(domSource, streamResult);
                baseValue++;
            } catch (Exception ignored) {
            }
        }
    }

    private static String getValueAfterLastDash(String initialValue) {
        return initialValue.substring(initialValue.lastIndexOf('-') + 1, initialValue.length());
    }

    private static String getValueBeforeLastDash(String initialValue) {
        return initialValue.substring(0, initialValue.lastIndexOf('-') + 1);
    }
}

请展示你已经做过的tried@mnwsmit:-请现在检查。请显示您已经完成的内容tried@mnwsmit:-请现在检查。与其简单地粘贴一段代码,我认为如果你能在代码中添加一个简短的描述,说明你编写、更改或添加的内容,那将是一件好事,甚至可能是你为什么这么做。@sdotdi我很难用这个网站来回答。最后我和普拉纳夫通了电话!,一旦我明白怎么做,就会更新答案!与其只是简单地粘贴一段代码,我认为如果你能在代码中添加一个简短的描述,说明你写了什么、修改了什么或添加了什么,甚至说明你为什么要这么做,那就太好了。@sdotdi我很难用这个网站来回答这个问题。最后我和普拉纳夫通了电话!,一旦我明白怎么做,就会更新答案!