Last active
October 16, 2020 12:36
-
-
Save aarchilla84/5229008 to your computer and use it in GitHub Desktop.
Lectura de un fichero XML a través de expresiones xPath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<parameters> | |
<parameter name="parametro1" class="parametroGeneral"> | |
<parameterDescription>Parametro general de pruebas 1</parameterDescription> | |
</parameter> | |
</parameters> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Se crea la instancia del builder necesario para leer el fichero xml | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
// Se lee el fichero xml | |
Document document = builder.parse(getClass().getResourceAsStream("path/al/fichero.xml")); | |
// Instancia de xPath necesaria para compilar las expresiones | |
XPath xpath = XPathFactory.newInstance().newXPath(); | |
XPathExpression exprParameter = xpath.compile("//parameter"); | |
/* Una vez compilada la expresión se evalua teniendo en cuenta que el resultado pueden ser múltiples nodos. | |
* Si la expresión hace referéncia a un nodo aunque sepamos que sólo hay uno en el documento, el tipo de retorno será un NodeList | |
* por lo que se tiene que indicar XPathConstants.NODESET al invocar el evaluate | |
*/ | |
NodeList parameters = (NodeList)exprParameter.evaluate(document, XPathConstants.NODESET); | |
// Como en la evaluación se ha indicado que es posible que haya más de un nodo el resultado es una lista | |
for(int i=0; i<parameters.getLength(); i++){ | |
// Se recogen los diferentes valores y se muestran por pantalla | |
Node parameterNode = parameters.item(i); | |
String parameterName = parameterNode.getAttributes().getNamedItem("name").getNodeValue(); | |
String parameterClass = parameterNode.getAttributes().getNamedItem("class").getNodeValue(); | |
System.out.println("Leido parámetro " + parameterName + " de clase " + parameterClass); | |
} | |
/* A diferencia de la expresión anterior, esta hace referencia al texto que hay dentro del tag parameterDescription, | |
* indicado en la expresión por la función text(), por lo que al evaluarla se tiene que indicar XPathConstants.STRING | |
* porque el tipo de retorno es una String | |
*/ | |
XPathExpression expreDescription = xpath.compile("//parameter[@name='"+ parameterName +"']/parameterDescription/text()"); | |
String parameterDescription = (String)expreDescription.evaluate(document, XPathConstants.STRING); | |
System.out.println("La descripción del parámetro 'parametro1' es " + parameterDescription); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment