Error: Can not find symbol getRootElement [closed]

1

I'm trying to read a file .xml using the code below

  public static String lerArquivoXML(String string){
        SAXBuilder builder = new SAXBuilder();
    File xmlFile = new File("c:\teste.xml");

    try {

            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren("staff");
        for (int i = 0; i < list.size(); i++) {
           Element node = (Element) list.get(i);
           System.out.println("First Name : " + node.getChildText("firstname"));
           System.out.println("Last Name : " + node.getChildText("lastname"));
           System.out.println("Nick Name : " + node.getChildText("nickname"));
           System.out.println("Salary : " + node.getChildText("salary"));
        }
      } catch (IOException io) {
        System.out.println(io.getMessage());
      } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
      }  
        return null;    
    }

However, I have an error return on line 8 ( Element rootNode = document.getRootElement(); ) according to the question title. The imports are Ok

xml used

  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:apis="http://schemas.datacontract.org/2004/07/Integracao.Modelo.Chamada">
           <soapenv:Body>
            <tem:CancelaSP>
               <!--Optional:-->
               <tem:token>386922849949</tem:token>
               <!--Optional:-->
               <tem:cancelaSPIntegracao>
                  <!--Optional:-->
                  <apis:AnoSP>2016</apis:AnoSP>
                  <!--Optional:-->
                  <apis:NumeroSP>5656</apis:NumeroSP>
               </tem:cancelaSPIntegracao>
            </tem:CancelaSP>
         </soapenv:Body>
      </soapenv:Envelope>
    
asked by anonymous 22.12.2016 / 15:06

1 answer

0

Since you did not explain exactly what's happening with the code, I did the same approach as you, but I used standard JDK methods, I did not use SAX . And this approach seemed simpler to me. Feel free to say is not what you want , which I'll redo using SAX .

  

test.xml

<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:apis="http://schemas.datacontract.org/2004/07/Integracao.Modelo.Chamada">
           <soapenv:Body>
            <tem:CancelaSP>
               <!--Optional:-->
               <tem:token>386922849949</tem:token>
               <!--Optional:-->
               <tem:cancelaSPIntegracao>
                  <!--Optional:-->
                  <apis:AnoSP>2016</apis:AnoSP>
                  <!--Optional:-->
                  <apis:NumeroSP>5656</apis:NumeroSP>
               </tem:cancelaSPIntegracao>
            </tem:CancelaSP>
         </soapenv:Body>
</soapenv:Envelope>
  

Application

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class JavaApplication2 {

    public static void main(String[] args) {
        try {

            File fXmlFile = new File("/res/teste.xml"); // C:/.../teste.xml

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dbFactory.newDocumentBuilder();
            Document doc = builder.parse(fXmlFile);

            //doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("soapenv:Envelope");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node mNode = nodeList.item(i);

                if (mNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element mElement = (Element) mNode;

                    System.out.println("AnoSP: " + mElement.getElementsByTagName("apis:AnoSP").item(0).getTextContent());
                    System.out.println("NumeroSP: " + mElement.getElementsByTagName("apis:NumeroSP").item(0).getTextContent());


                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
  

Output

AnoSP: 2016
NumeroSP: 5656
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)

The above code causes you to get the value of any Tag that is inside the tag: soapenv:Envelope .

    
22.12.2016 / 16:08