Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import javax.xml.parsers.DocumentBuilder;
  2. import javax.xml.parsers.DocumentBuilderFactory;
  3. import javax.xml.xpath.XPath;
  4. import javax.xml.xpath.XPathConstants;
  5. import javax.xml.xpath.XPathExpression;
  6. import javax.xml.xpath.XPathFactory;
  7.  
  8. import org.w3c.dom.Document;
  9. import org.w3c.dom.Element;
  10. import org.w3c.dom.NodeList;
  11.  
  12.  
  13. public class Main
  14. {
  15.     public static void main(String[] args) throws Exception
  16.     {
  17.         DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
  18.         domFactory.setNamespaceAware(true);
  19.         DocumentBuilder builder = domFactory.newDocumentBuilder();
  20.         Document doc = builder.parse("input.xml");
  21.        
  22.         XPath xpath = XPathFactory.newInstance().newXPath();
  23.        
  24.         XPathExpression expr = xpath.compile("/stress_test/protocollo/utenti");
  25.         Object result = expr.evaluate(doc, XPathConstants.NODESET);
  26.        
  27.         NodeList nodes = (NodeList) result;
  28.         System.out.println("Ho trovato " + nodes.getLength() + " nodi");
  29.        
  30.         for (int i = 0; i < nodes.getLength(); i++)
  31.         {
  32.             Element utente = (Element) nodes.item(i);
  33.            
  34.             String username = xpath.evaluate("utente/username", utente);
  35.             String password = xpath.evaluate("utente/password", utente);
  36.            
  37.             System.out.println(username + " " + password);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement