3 回答
TA贡献1797条经验 获得超6个赞
怎么样:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class Demo {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("input.xml"));
NodeList nodeList = document.getElementsByTagName("Item");
for(int x=0,size= nodeList.getLength(); x<size; x++) {
System.out.println(nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue());
}
}
}
TA贡献1797条经验 获得超4个赞
下面是在vtd-xml中执行此操作的代码。它基本上使用“ / xml / item / @ name”的XPath查询XML。
import com.ximpleware.*;
public class getAttrs{
public static void main(String[] s) throws VTDException{
VTDGen vg = new VTDGen();
if (!vg.parseFile("input.xml",false)) // turn off namespace
return;
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/xml/item/@name");
int i=0;
while( (i=ap.evalXPath())!=-1){
System.out.println(" item name is ===>"+vn.toString(i+1));
}
}
}
添加回答
举报