简体   繁体   中英

Java + reading data from XML file

What's wrong in this code? It gives me null everytime. I have no idea how to repair it, because in ordinary Java Application it works.

    @WebMethod(operationName = "getColor")
    public String getColor(@WebParam(name = "regNr") String regNr) {
        String kolor=null;
    try {

    File fXmlFile = new File("/base.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();

    NodeList nList = doc.getElementsByTagName("person");


    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
                 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element element = (Element) nNode;
                        String id = ""+ getValue("id",element);


        if (regNr.equals(id)) {
                    color = element.getElementsByTagName("color").item(0).getTextContent();
                    return color;

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

我认为(不确定(因为我不知道它在什么时候给您提供null)),您必须删除/ form文件名,使其仅成为base.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM