Error: Cannot find Symbol getRootElement

Asked

Viewed 67 times

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 title of the question. 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>
  • 1

    Put which is line 8 in your question.

  • 1

    @Taisbevalle edited

1 answer

0


Since you didn’t explain exactly what’s going on with the code, I took the same approach as you, but I used standard JDK methods, I didn’t use SAX. And this approach seemed simpler to me. Feel free to say that it’s not what you want, which I will redo using SAX.

xml test.

<?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)

inserir a descrição da imagem aqui

The code above makes you can pick up the value of any Tag that is within the tag: soapenv:Envelope.

  • With your code, the error goes to getelementsbytagname

  • How are you doing? With the code I sent you? Put it in Pastebin.

  • I used exactly its function, without changing anything. I just corrected the imports

  • Weird. Changed the tag modelo for the tag of your xml?

  • Put the xml you’re using in the question, I’ll try to do something based on it.

  • I will edit the question with my xml example

  • I made the code with the example of your XML.

  • The error remained. What are the Imports to do? see http://imgur.com/a/PLhNK

  • I added the imports, don’t forget to create a folder on C:// as an example C:/teste and put the . xml file there, or else put it in the project and change the path string

  • With the imports the error has been corrected, although the function is not returning anything

  • Look down there return null. Does a string to return the result, something like String str; ... inside the loop: str = mElement.getElementsByTagName("apis:AnoSP").item(0).getTextContent();

  • does not return anything because its function returns null

  • Just in the picture. I changed the kind of return, forgiveness rs

  • systemOutprint n displays nothing, understand?

  • Yes I do. Strange. How are you calling the method?

  • 1

    Solved. I debugged and for some reason I wasn’t entering the loop. I recreated the code and it worked.

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.