2014-01-21 131 views
2

如何在java或C中使用vtd-xml解析xml文件,如下所示:使用vtd-xml解析xml文件

<?xml version="1.0" encoding="utf-8"?> 
<StockReport> 
    <Article Code="027783012" Height="35" Width="36" Length="136" TotalPacks="4" AvailablePacks="4" StockReturnPacks="4" BlockedPacks="0" NextExpiryDate="2015-01-17"> 
     <Machine Number="1" TotalPacks="4" AvailablePacks="4" StockReturnPacks="4" BlockedPacks="0" NextExpiryDate="2015-01-17" /> 
</Article> 
<Article Code="025349109" Height="36" Width="37" Length="129" TotalPacks="6" AvailablePacks="6" StockReturnPacks="6" BlockedPacks="0" NextExpiryDate="2015-01-17"> 
     <Machine Number="1" TotalPacks="6" AvailablePacks="6" StockReturnPacks="6" BlockedPacks="0" NextExpiryDate="2015-01-17" /> 
</Article> 
<Article Code="039154327" Height="0" Width="0" Length="0" TotalPacks="0" AvailablePacks="0" StockReturnPacks="0" BlockedPacks="0" NextExpiryDate="" /> 
<Article Code="932654167" Height="57" Width="99" Length="137" TotalPacks="27" AvailablePacks="27" StockReturnPacks="27" BlockedPacks="0" NextExpiryDate="2014-04-17"> 
    <Machine Number="1" TotalPacks="16" AvailablePacks="16" StockReturnPacks="16" BlockedPacks="0" NextExpiryDate="2015-01-17" /> 
    <Machine Number="2" TotalPacks="11" AvailablePacks="11" StockReturnPacks="11" BlockedPacks="0" NextExpiryDate="2014-04-17" /> 
</Article> 
</StockReport> 

任何幫助,將不勝感激。

謝謝

+0

你看過java和C中的教程嗎?這應該很容易... –

回答

7

我想這其中的一些取決於你想如何解析文件。

這裏有一個「非生產」的例子,它使用一些技術,這是有用的,包括:

  • XPath的選擇(只使用「/ *」這裏)
  • 去翻所有兄弟節點
  • 看不起通過子節點
  • 提取節點屬性到自動駕駛儀使用一個
  • 地圖

希望它可以幫助

package scce; 

import com.ximpleware.AutoPilot; 
import com.ximpleware.NavException; 
import com.ximpleware.VTDGen; 
import com.ximpleware.VTDNav; 
import com.ximpleware.XPathEvalException; 
import com.ximpleware.XPathParseException; 
import java.io.File; 
import java.io.IOException; 
import java.util.LinkedHashMap; 
import java.util.Map; 

/** 
* 
* @author David 
*/ 
public class VTDParserExample { 

    VTDGen vg; 
    VTDNav vn; 

    public VTDParserExample() { 
     vg = new VTDGen();  
    } 

    public void parseAndPrint() throws NavException { 

     int level = 0; 

     for(boolean el = (vn != null); 
        el == true ; 
        el = vn.toElement(VTDNav.NEXT_SIBLING)) { 

      printTag(vn, level); 

      parseAndPrintChildren(level);     
     } 

    } 

    private void parseAndPrintChildren(int level) throws NavException { 

     vn.push(); 

     for(boolean el = vn.toElement(VTDNav.FIRST_CHILD); 
        el == true ; 
        el = vn.toElement(VTDNav.NEXT_SIBLING)) { 

      printTag(vn, level + 1); 

      parseAndPrintChildren(level + 1);     
     } 

     vn.pop(); 

    } 

    public VTDNav loadFile(String filePath) throws IOException { 

     File fDoc = new File(filePath); 

     if (fDoc != null && fDoc.exists()) { 
      System.out.println("loadFile file exists ["+filePath+"]"); 

      vg.clear(); 
      if (vg.parseFile(filePath, true)) { 
       vn = vg.getNav(); 
      } 
     } 
     else { 
      throw new IOException("File ["+filePath+"] invalid"); 
     }    

     if (vn == null) {     
      throw new IOException("Cannot parse file ["+filePath+"]"); 
     } 

     return vn; 
    } 

    public void getElementsByXpath() { 

     AutoPilot ap = new AutoPilot(vn); 

     try 
     { 
      String xpQ = "/*"; 

      ap.selectXPath(xpQ);  
      if (ap.evalXPathToBoolean()) { 
       ap.evalXPath(); 
      } 
      else { 
       System.out.println(this.getClass()+".getAllElements evalXPathToBoolean["+ap.evalXPathToBoolean()+"]"); 
      } 
     } 
     catch(XPathParseException | XPathEvalException | NavException e) { 
      e.printStackTrace(); 
     }    

    } 

    private void loadAttributeMap(VTDNav nav, Map<String, String>amap) {  

     nav.push(); 

     try { 
      AutoPilot apAtt = new AutoPilot(nav); 
      apAtt.selectXPath("@*"); 

      int j=-1; 
      while ((j=apAtt.evalXPath())!=-1) { 
       String name = nav.toString(j); 
       String val = nav.toString(j+1); 

       amap.put(name, val); 
      }      
     } 
     catch(XPathParseException | XPathEvalException | NavException e) { 
      e.printStackTrace(); 
     } 

     nav.pop();   
    }  

    private void printTag(VTDNav vn, int level) throws NavException { 

     String tag = vn.toString(vn.getCurrentIndex()); 
     System.out.print("Level ["+level+"] Tag ["+tag+"]"); 

     Map<String, String>amap = new LinkedHashMap<String, String>(); 

     loadAttributeMap(vn, amap); 

     for (String aname: amap.keySet()) { 
      String aval = amap.get(aname); 

      System.out.print(" @"+aname+"="+aval); 
     } 
     System.out.print("\n"); 
    } 

    public static void main(String[] args) { 

     VTDParserExample vp = new VTDParserExample(); 

     try { 

      vp.loadFile("src/scce/famedoro.xml");          
      vp.getElementsByXpath(); 
      vp.parseAndPrint(); 

     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

哪產生以下輸出:

loadFile file exists [src/scce/famedoro.xml] 
Level [0] Tag [StockReport] 
Level [1] Tag [Article] @Code=027783012 @Height=35 @Width=36 @Length=136 @TotalPacks=4 @AvailablePacks=4 @StockReturnPacks=4 @BlockedPacks=0 @NextExpiryDate=2015-01-17 
Level [2] Tag [Machine] @Number=1 @TotalPacks=4 @AvailablePacks=4 @StockReturnPacks=4 @BlockedPacks=0 @NextExpiryDate=2015-01-17 
Level [1] Tag [Article] @Code=025349109 @Height=36 @Width=37 @Length=129 @TotalPacks=6 @AvailablePacks=6 @StockReturnPacks=6 @BlockedPacks=0 @NextExpiryDate=2015-01-17 
Level [2] Tag [Machine] @Number=1 @TotalPacks=6 @AvailablePacks=6 @StockReturnPacks=6 @BlockedPacks=0 @NextExpiryDate=2015-01-17 
Level [1] Tag [Article] @Code=039154327 @Height=0 @Width=0 @Length=0 @TotalPacks=0 @AvailablePacks=0 @StockReturnPacks=0 @BlockedPacks=0 @NextExpiryDate= 
Level [1] Tag [Article] @Code=932654167 @Height=57 @Width=99 @Length=137 @TotalPacks=27 @AvailablePacks=27 @StockReturnPacks=27 @BlockedPacks=0 @NextExpiryDate=2014-04-17 
Level [2] Tag [Machine] @Number=1 @TotalPacks=16 @AvailablePacks=16 @StockReturnPacks=16 @BlockedPacks=0 @NextExpiryDate=2015-01-17 
Level [2] Tag [Machine] @Number=2 @TotalPacks=11 @AvailablePacks=11 @StockReturnPacks=11 @BlockedPacks=0 @NextExpiryDate=2014-04-17 

添加自動駕駛儀環路示例(而不是基於如上純粹節點)不混合使用這些很好

package scce; 

import com.ximpleware.AutoPilot; 
import com.ximpleware.NavException; 
import com.ximpleware.VTDGen; 
import com.ximpleware.VTDNav; 
import com.ximpleware.XPathEvalException; 
import com.ximpleware.XPathParseException; 
import java.io.File; 
import java.io.IOException; 
import java.util.LinkedHashMap; 
import java.util.Map; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author David 
*/ 
public class VTDParserExample { 

    VTDGen vg; 
    VTDNav vn; 
    AutoPilot ap; 

    public VTDParserExample() { 
     vg = new VTDGen();  
    } 

    public void parseAndPrint() throws NavException { 

     int level = 0; 

     for(boolean el = (vn != null); 
        el == true ; 
        el = vn.toElement(VTDNav.NEXT_SIBLING)) { 

      printTag(vn, level); 

      parseAndPrintChildren(level);     
     } 

    } 

    private void parseAndPrintChildren(int level) throws NavException { 

     vn.push(); 

     for(boolean el = vn.toElement(VTDNav.FIRST_CHILD); 
        el == true ; 
        el = vn.toElement(VTDNav.NEXT_SIBLING)) { 

      printTag(vn, level + 1); 

      parseAndPrintChildren(level + 1);     
     } 

     vn.pop(); 

    } 

    private VTDNav loadFile(String filePath) throws IOException { 

     File fDoc = new File(filePath); 

     if (fDoc != null && fDoc.exists()) { 
      System.out.println("loadFile file exists ["+filePath+"]"); 

      vg.clear(); 
      if (vg.parseFile(filePath, true)) { 
       vn = vg.getNav(); 
      } 
     } 
     else { 
      throw new IOException("File ["+filePath+"] invalid"); 
     }    

     if (vn == null) {     
      throw new IOException("Cannot parse file ["+filePath+"]"); 
     } 

     return vn; 
    } 

    public boolean getElementsByXpath() { 
     boolean found = false; 

     ap = new AutoPilot(vn); 

     try 
     { 
      String xpQ = "//Machine"; 

      ap.selectXPath(xpQ);  
      if (ap.evalXPathToBoolean()) { 
       found = true; 
      } 
      else { 
       System.out.println(this.getClass()+".getAllElements evalXPathToBoolean["+ap.evalXPathToBoolean()+"]"); 
      } 
     } 
     catch(XPathParseException e) { 
      e.printStackTrace(); 
     }    

     return found; 
    } 

    private void loadAttributeMap(VTDNav nav, Map<String, String>amap) {  

     nav.push(); 

     try { 
      AutoPilot apAtt = new AutoPilot(nav); 
      apAtt.selectXPath("@*"); 

      int j=-1; 
      while ((j=apAtt.evalXPath())!=-1) { 
       String name = nav.toString(j); 
       String val = nav.toString(j+1); 

       amap.put(name, val); 
      }      
     } 
     catch(XPathParseException | XPathEvalException | NavException e) { 
      e.printStackTrace(); 
     } 

     nav.pop();   
    }  

    private void printTag(VTDNav vn, int level) throws NavException { 

     String tag = vn.toString(vn.getCurrentIndex()); 
     System.out.print("Level ["+level+"] Tag ["+tag+"]"); 

     Map<String, String>amap = new LinkedHashMap<String, String>(); 

     loadAttributeMap(vn, amap); 

     for (String aname: amap.keySet()) { 
      String aval = amap.get(aname); 

      System.out.print(" @"+aname+"="+aval); 
     } 
     System.out.print("\n"); 
    } 

    public static void main(String[] args) { 

     VTDParserExample vp = new VTDParserExample(); 

     try { 

      vp.loadFile("src/scce/famedoro.xml"); 
      if (vp.getElementsByXpath()) { 
       vp.parseAndPrintAP();     
      } 
     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    private void parseAndPrintAP() { 
     int level = 0; 
     int result = -1; 

     try { 
      while((result = ap.evalXPath())!=-1){ 
       printTag(vn, level); 

       parseAndPrintChildren(level);     

      } 
     } catch (XPathEvalException | NavException ex) { 
      ex.printStackTrace(); 
     } 
    } 

} 

這 - 將不同的XPath設置爲「// Machine」生成:

loadFile file exists [src/scce/famedoro.xml] 
Level [0] Tag [Machine] @Number=1 @TotalPacks=4 @AvailablePacks=4 @StockReturnPacks=4 @BlockedPacks=0 @NextExpiryDate=2015-01-17 
Level [0] Tag [Machine] @Number=1 @TotalPacks=6 @AvailablePacks=6 @StockReturnPacks=6 @BlockedPacks=0 @NextExpiryDate=2015-01-17 
Level [0] Tag [Machine] @Number=1 @TotalPacks=16 @AvailablePacks=16 @StockReturnPacks=16 @BlockedPacks=0 @NextExpiryDate=2015-01-17 
Level [0] Tag [Machine] @Number=2 @TotalPacks=11 @AvailablePacks=11 @StockReturnPacks=11 @BlockedPacks=0 @NextExpiryDate=2014-04-17 
+0

感謝您的友好迴應,但我對「public void getAllElements()」方法有所懷疑,這是正確的嗎?什麼是xpNum? – famedoro

+1

是的,再看一遍,有幾個問題1)它沒有在main中調用,2)它周圍不應該有push/pop - 我認爲這會拋出xpath輸出。我將編輯我的答案 – Dazed

+0

我已將「getAllElements」重命名爲「getElementsByXpath」(可能更相關 - 並將「xpNum」引用更改爲「evalXPathToBoolean」,這是從另一個示例掛起的。 – Dazed