how to get the attribute value of an xml node using java

前端 未结 6 829
抹茶落季
抹茶落季 2020-12-03 13:59

I\'ve an xml which looks like this:

{ .....         


        
6条回答
  •  甜味超标
    2020-12-03 14:36

    Since your question is more generic so try to implement it with XML Parsers available in Java .If you need it in specific to parsers, update your code here what you have tried yet

    
    
        TEST
        
    
    
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("uri to xmlfile");
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile("//ep/source[@type]");
    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    
    for (int i = 0; i < nl.getLength(); i++)
    {
        Node currentItem = nl.item(i);
        String key = currentItem.getAttributes().getNamedItem("type").getNodeValue();
        System.out.println(key);
    }
    

提交回复
热议问题