Search code examples
javastringstringbuilderxmlreader

stringbuilder in for loop with if statement


I have a problem with my java program which reads my xml file and show me the content I want.... i have 3 lines in there and it shows me with the System.out.println also these 3 lines with the specific content I want from.

But I have to return these line for line as a String. And here is the problem in my other class where i Uses the return String I get only the first line of my xml file. So i think I have a problem with the return value and the for loop. Here is my code with the java program where it works with the system.out.println....

package org.java_websocket.xmlreader;

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 websocketxmlrader {
     public static void main(String argv[]){

            try {

            File fXmlFile = new File("...test123.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);


            doc.getDocumentElement().normalize();


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

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);


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

                    Element eElement = (Element) nNode;

                    String s1 = "{\"art.\":1234,";
                    String s2 = "\"value\":5678,";
                    String s3 = "\"set\":null,";
                    String s4 = "\"condition\":" + eElement.getElementsByTagName("length").item(0).getTextContent();
                    String s5=  "\"height\"" + eElement.getElementsByTagName("weight").item(0).getTextContent();
                    String s6= "\"average\":5678,";
                    String s7= "\"content\":12,";
                    String s8= "\"div.\":78}";
                    String s = s1+s2+s3+s4+","+s5+","+s6+s7+s8;
                    System.out.println(s);

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


}

and this is the file where I create a return value from the string

I change here from public static void main(String argv[]){ ..... to .. public String wert(){....

Here is the full code

        package packageWebSocket;

    import java.io.File;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

    public class websocketxmlrader {
          public  String wert(){
     try {

                File fXmlFile = new File("...test123.xml");
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);


                doc.getDocumentElement().normalize();


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

                for (int temp = 0; temp < nList.getLength(); temp++) {

                    Node nNode = nList.item(temp);


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

                        Element eElement = (Element) nNode;

                        String s1 = "{\"art.\":1234,";
                        String s2 = "\"value\":5678,";
                        String s3 = "\"set\":null,";
                        String s4 = "\"condition\":" + eElement.getElementsByTagName("length").item(0).getTextContent();
                        String s5=  "\"height\"" + eElement.getElementsByTagName("weight").item(0).getTextContent();
                        String s6= "\"average\":5678,";
                        String s7= "\"content\":12,";
                        String s8= "\"div.\":78}";
                        String s = s1+s2+s3+s4+","+s5+","+s6+s7+s8;

                        return s;
    }



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

                }
            return null;
    }
}

How can I use here a Stringbuilder I think this will solve the problem. Because the return String should also loop through every line in the xml file and not only return a string from the first line.

Thank You !


Solution

  • If you reach a return you leave the method. So if you find a node with type Node.ELEMENT_NODE you build your String s, return this value and leave the method.

    You may add a linebreak instead of returning the value, or (if it is allowed to change the return type) use List<String> and add for each line s to the list.

    StringBuild sb = new StringBuilder(); // with linebreaks
    List<String> lines = new ArrayList<>(); // each line one entry
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            String s = ...;
            if (sb.length() > 0) // add linebreak
                sb.append(System.getProperty("line.separator"));
            sb.append(s);
            lines.add(s);
        }
    }