簡體   English   中英

使用vtd-xml和xPath修改XMLSchema位置

[英]Modify XMLSchema Location with vtd-xml and xPath

有沒有辦法修改vtd-xml解析的XML模式位置?

xml文件看起來像

<rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xmlSchema.xsd">
.
.
.
 </rootElement>

最好的解決方案是使用xPath。 我試過了

/rootElement/@*

與DOM一起使用,但與vtd-xml不一起使用

這是我的代碼

String xPath = "/rootElement/@*"
XMLModifier xm = new XMLModifier();
VTDGen vg = new VTDGen();
if (vg.parseFile(fnIn,true)){
       VTDNav vn = vg.getNav();
       xm.bind(vn);
       nodeXpath(xPath,vn);
}

private void nodeXpath(String xPath, VTDNav vn) throws Exception{
    int result;

    AutoPilot ap = new AutoPilot();
    ap.selectXPath(xPath);
    ap.bind(vn);
    while((result = ap.evalXPath())!=-1){
    System.out.println(vn.getAttrVal("xsi:noNamespaceSchemaLocation") + ", " + vn.getText() + ", " + vn.toString(result));
    xm.updateToken(result,"test");
        int p = vn.getText();

        if (p!=-1) {                
            System.out.println(vn.getText() + ", " + vn.toString(p));               
        }
    }
}

打印到屏幕上的結果是:10,-1,xsi:noNamespaceSchemaLocation

並且輸出xml是

test
<rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" test="xmlSchema.xsd">

元素名稱似乎已被修改,並且頂部還有其他測試? 我的錯誤在哪里? 如何更改屬性值?

謝謝你的幫助。

好的,這是我對您的代碼的修改。 結果是屬性名稱標記索引。 並且“結果+1”應該是屬性值令牌索引。 讓我知道它是否對您有用。

AutoPilot ap = new AutoPilot();
ap.selectXPath(xPath);
ap.bind(vn);
while((result = ap.evalXPath())!=-1){
// System.out.println(vn.getAttrVal("xsi:noNamespaceSchemaLocation") + ", " + vn.getText() + ", " + vn.toString(result));
xm.updateToken(result_+1,"test");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM