1

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 !

Hans
  • 25
  • 6
  • 1
    Can you show us only required code instead of whole code? – Maulik Doshi May 02 '17 at 09:39
  • 1
    Perhaps you would like to [fix your indentation](http://stackoverflow.com/posts/43734534/edit) to make it easier for people to help you. – khelwood May 02 '17 at 09:40
  • 1
    Try that "research" thing. There are tons of links that show how to use a real StringBuilder (like [here](http://stackoverflow.com/questions/8725739/correct-way-to-use-stringbuilder) ) – GhostCat May 02 '17 at 09:42

1 Answers1

0

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);
    }
}
Stefan Warminski
  • 1,282
  • 6
  • 14
  • and the error is ... ? (sorry, my crystal ball is broken) – Stefan Warminski May 02 '17 at 10:50
  • Multiple markers at this line - ArrayList cannot be resolved to a type - List cannot be resolved to a type - The type List is not generic; it cannot be parameterized with arguments – Hans May 02 '17 at 10:57
  • You need to import java.util.List and java.util.ArrayList. – Stefan Warminski May 02 '17 at 11:01
  • yes this works, but when i run my other java program where i load the return value , i get null ... because i have in the program where i insert your code at the end ... return null; you can see it in my second code above . – Hans May 02 '17 at 11:19
  • Of course you need the change this line. If you want a single string it is `return sb.toString()`. If you can change the return value you simply `return lines`. – Stefan Warminski May 02 '17 at 11:23