简体   繁体   English

使用带有vtd-xml Java的XPath更改xml属性值

[英]Change xml attribute value with XPath with vtd-xml Java

I have to set the value of "count" attribute in this xml: 我必须在此xml中设置“count”属性的值:

<?xml version="1.0" encoding="UTF-8"?>
<task>
   <trigger count="myCount" interval="myInterval"/>
   <property name="myName" value="myValue"/>
   <property name="mySecondName"value="mySecondValue">
</task>

i'd like to change myCount value with "Foo" with a code like this (VTDXML library): 我想用“Foo”用这样的代码(VTDXML库)更改myCount值:

            String count  = "Foo";
            if (vg.parseFile("C:\\Users\\_myPath_\\myFile.xml", true)) {
                VTDNav vn = vg.getNav();
                ap.bind(vn);
                xm.bind(vn);
                ap.selectXPath("/*[name()='task']/*[name()='trigger']");
                int i=0;
                while((i=ap.evalXPath())!=-1){
                    xm.insertAfterHead(count);
                }
                xm.output("C:\\Users\\_myPath_\\myFileWithFoo.xml");
                System.out.println(vg);
            }

In this way i obtain instead 通过这种方式,我获得了

  <trigger count="myCount" interval="myInterval">Foo</trigger>

that is not my goal, because what i want is 这不是我的目标,因为我想要的是

  <trigger count="Foo" interval="myInterval"/>

I found this solution applied for changing the content of both "count" and "interval": 我发现这个解决方案适用于更改“count”和“interval”的内容:

           String count= "Foo";
           String interval= "Dummy";       
           String attribute  = " count=\""+ foo + "\" interval=\""+ interval+"\"";
            if (vg.parseFile("C:\\Users\\_myPath_\\myFile.xml", true)) {
                VTDNav vn = vg.getNav();
                ap.bind(vn);
                xm.bind(vn);
                ap.selectXPath("/*[name()='task']/*[name()='trigger']");
                int i=0;
                while((i=ap.evalXPath())!=-1){
                    xm.insertAttribute(attribute);
                }
                xm.output("C:\\Users\\_myPath_\\myFileWithFoo.xml");
                System.out.println(vg+attribute);
            }

And the result is: 结果是:

 <trigger count="Foo" interval="Dummy" />

I used method insertAttribute that appends my string to the name of the node (trigger). 我使用方法insertAttribute将我的字符串追加到节点的名称(触发器)。

I know this is an horrible solution, but it works fine. 我知道这是一个可怕的解决方案,但它工作正常。

Your xpath should be /task/trigger/@count 你的xpath应该是/ task / trigger / @ count

the statement for change attr value is xmlModifier.updateToken(i+1) 更改attr值的语句是xmlModifier.updateToken(i + 1)

Below is a sample without resorting to namespaces... 下面是一个不使用命名空间的示例...

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import com.ximpleware.*;

public class updateAttrVal2 {
    public static void main(String[] s) throws VTDException,UnsupportedEncodingException,IOException{
        VTDGen vg = new VTDGen();
        String xml="<task xmlns='ns1' xmlns:abc='ns2'><abc:trigger count=\"myCount\" interval=\"myInterval\"/></task>";
        vg.setDoc(xml.getBytes());
        vg.parse(false);
        VTDNav vn=vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        XMLModifier xm = new XMLModifier(vn);
        ap.selectXPath("/task/trigger/@count");
        int i=0;
        while((i=ap.evalXPath())!=-1){
            xm.updateToken(i+1, "Count");
        }
        XMLByteOutputStream xms = new XMLByteOutputStream(xm.getUpdatedDocumentSize());
        xm.output(xms);
        System.out.println(xms.toString());
    }
}

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

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