我如何在Java或C中使用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>
任何帮助,将不胜感激。
谢谢
我想其中有些取决于您要如何解析文件。
这是一个“非生产”示例,其中使用了一些有用的技术,包括:
希望能帮助到你
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
添加带有AutoPilot循环的示例(而不是如上所述的纯粹基于节点的示例)并不能很好地混合这些示例
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
如何在java或C中使用vtd xml解析如下xml文件? 任何帮助都将不胜感激。 谢谢
为了在巨大的xml文件中执行XPATH查询,我阅读了许多喜欢VTD-xml的文章,因此我复制了这些文章中的代码: 但当我运行它时没有结果,所以这意味着XML文件没有映射到内存中。。。我的问题是如何在VTD-xml中强制映射xml文件?
我必须对VTD-XML库进行性能测试,以便不仅进行简单的解析,而且在解析中进行额外的转换。所以我有30MB的输入XML,然后用自定义逻辑将其转换为其他XML。因此,我想消除所有的想法,这减缓了整个过程,从我这边来(因为没有很好地使用VTD库)。我试图搜索优化提示,但找不到。我认为: '0'. 选择selectXPath或selectElement最好使用什么? > 使用不带名称空间的解析要快得多。
我对如何做到这一点有点困惑,所有的文档/示例都展示了如何读取和编辑xml文档,但似乎没有任何从头开始创建xml的明确方法,我宁愿不必将我的程序与虚拟xml文件一起发布以编辑一个。有什么想法吗?谢谢。
VTD-XML是一种新的XML处理API,它能够克服DOM和SAX的一些问题。VTD-XML能够在内存中对XML进行快速检查并提供XPath查询支持。 在它的主页上有详细的API描述,源代码,文档和例子。
我有两个xml文件。一个是参考(旧)文件,另一个是测试(新)文件。根据提供给我的一些规则,我必须检查是否有东西从旧模型中删除,然后添加到新模型中,或者检查是否有东西从旧文件中删除到新文件中。 我使用的是VTD-XML,但DOM解决方案或任何其他与xpath一起使用的解决方案将非常有用。 这是java代码: 1) 在ref文件上完成xpath后,我得到man节点的所有属性: 我得到name属性的值。