Manipulating XML with java

0

It has a file .xml that has as main element (test cases) and as "daughters" the test case:

<casosTeste>
    <casoTeste nome="NOME01">
        <regex>TEXTO DE EXEMPLO 1</regex>
    </casoTeste>

    <casoTeste nome="NOME02">
        <regex>TEXTO DE EXEMPLO 1</regex>
        <regex>TEXTO DE EXEMPLO 2</regex>
        <regex>TEXTO DE EXEMPLO 3</regex>
        <regex>TEXTO DE EXEMPLO 4</regex>
    </casoTeste>

    <casoTeste nome="NOME03">
        <regex>TEXTO DE EXEMPLO 1</regex>
        <regex>TEXTO DE EXEMPLO 2</regex>
    </casoTeste>

    <casoTeste nome="NOME04">
        <regex>TEXTO DE EXEMPLO 1</regex>
    </casoTeste>
</casosTeste>

I need to create a program that when receiving a String pontoVer compare with the "name" field of the XML and if they are the same I can find the "REGEX" that I need ( dadosEntrada )

public static boolean analisar(String dadosEntrada, String pontoVer, String caminhoXML) throws JDOMException, IOException
{
    File XML = new File(caminhoXML); 
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(XML);
    Element root = (Element) doc.getRootElement();
    List listaCasosTeste = root.getChildren();
    Iterator iter = listaCasosTeste.iterator();
    boolean ehvalido = false;
    while(iter.hasNext()) 
    {
        Element casoTeste = (Element) iter.next();
        if(casoTeste.getAttributeValue("nome").equals(pontoVer))
        {
            ehvalido = dadosEntrada.matches(casoTeste.getChildText("regex"));
        }
        System.out.println("Nome: " + casoTeste.getAttributeValue("nome"));
        System.out.println("Regex: " + casoTeste.getChildText("regex"));
        System.out.println();
    }   
    return ehvalido;
}

public static void main(String[] args) throws JDOMException, IOException 
{
    String dadosEntrada = "TEXTO DE EXEMPLO 2";
    String pontoVer = "NOME02";
    String caminhoXml = "src/resources/pac_casos_teste.xml";
    boolean ehvalido = analisar(dadosEntrada, pontoVer, caminhoXml);

    if(ehvalido)
        System.out.print("Passou");
    else 
        System.out.print("Não passou");
}
The problem is that I'm only able to save the first REGEX of each test case, so in the case of this example, I need the "TEXT EXAMPLE 2" (which is the second regex in case) the program does not find. I need to find a way to go through these regex that exist, however I do not know how to do this, that all fields have the same name (REGEX).

    
asked by anonymous 06.03.2018 / 18:32

0 answers