简体   繁体   English

使用SAX解析器修改xml文件

[英]modifing a xml file using SAX parser

Can we modify or update xml file using SAX parser. 我们可以使用SAX解析器修改或更新xml文件。 If yes, please provide me the sample code or any helpful link 如果是,请提供示例代码或任何有用的链接

my xml file looks like this 我的xml文件看起来像这样

<vertices>
<vertex>
<name>user1</name>
<type>Ashok</type>
<nickname>nickuser1</nickname>
</vertex>
</vertices>"

I want to change "user1" to say "user2". 我想将“user1”改为“user2”。 Help me out 帮帮我

If you are reluctant to use a DOM parser because you have a large XML you can use the XPATH or XLST to transform the xml. 如果您不愿意使用DOM解析器,因为您有一个大型XML,您可以使用XPATH或XLST来转换xml。

What is best way to change one value in XML files in Java? 在Java中更改XML文件中的一个值的最佳方法是什么?

Below is the code to do it in vtd-xml ... It is a lot more efficient/simpler than DOM or SAX based solutions... for further reading here is an article entitled manipulate XML the Ximple Way ... 下面是在vtd-xml中执行此操作的代码...它比基于DOM或SAX的解决方案更有效/更简单...在这里进一步阅读是一篇题为操纵XML的习惯方法的文章...

import java.io.*;
public class modifyXML {
    public static void main(String[] s) throws VTDException, IOException{
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/vertices/vertex/name/text()");
        XMLModifier xm = new XMLModifier(vn);
        // using XPath
        int i=ap.evalXPath();
        if(i!=-1){
            xm.updateToken(i, "user2");
        }
        xm.output("output.xml");
    }
}

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

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