Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在xml元素中获取属性值_Java_Xml - Fatal编程技术网

Java 在xml元素中获取属性值

Java 在xml元素中获取属性值,java,xml,Java,Xml,我有一个这样的xml字符串,我想在每个元素的循环中获得“name”的属性值。我该怎么做?我正在使用javax.xml.parsers库 历年 200 350 400 $320.00 670 8000 60 10 12 $250.00 假的 假的 立即的 不适用 这就是我一直在尝试的 DocumentBuilderFactory dbc = DocumentBuilderFactory.newInstance(); DocumentBuilder dbuilder;

我有一个这样的xml字符串,我想在每个元素的循环中获得“name”的属性值。我该怎么做?我正在使用javax.xml.parsers库


历年
200
350
400
$320.00
670
8000
60
10
12
$250.00
假的
假的
立即的
不适用
这就是我一直在尝试的

DocumentBuilderFactory dbc = DocumentBuilderFactory.newInstance();
        DocumentBuilder dbuilder;
        try {
            dbuilder = dbc.newDocumentBuilder();
            Document doc = dbuilder.parse(new InputSource(new StringReader(plan.getProvisions())));
            NodeList nl = doc.getElementsByTagName("Item");
            for(int i = 0 ; i < nl.getLength(); i++){
                if(i == row){                   
                    Element e = (Element)nl.item(i);
                    description = e.getAttribute("name");
                }
            }
        } catch (ParserConfigurationException e) {          
            e.printStackTrace();
        } catch (SAXException e) {          
            e.printStackTrace();
        } catch (IOException e) {           
            e.printStackTrace();
        }
DocumentBuilderFactory dbc=DocumentBuilderFactory.newInstance();
文档生成器dbuilder;
试一试{
dbuilder=dbc.newDocumentBuilder();
Document doc=dbuilder.parse(新的InputSource(新的StringReader(plan.getProvisions()));
NodeList nl=doc.getElementsByTagName(“项目”);
对于(int i=0;i
我想我明白了。我必须明确地使用
org.w3c.dom.Element
。我也有一个不同的元素字段。

怎么样:

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());
        }
    }
}
导入java.io.File;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
公开课演示{
公共静态void main(字符串[]args)引发异常{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
documentdocument=db.parse(新文件(“input.xml”);
NodeList NodeList=document.getElementsByTagName(“项”);

对于(int x=0,size=nodeList.getLength();x,下面是执行此操作的代码。它基本上是使用XPath“/XML/item/@name”查询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)); 
         }
   }
}