0

I'm using an existing XMLReader code in my project for taking elements from an XML page. It works fine for only one element like this:

    public String getURL(String Url) {

    BufferedReader br = null;

    try {

        URL url = new URL(Url);
        br = new BufferedReader(new InputStreamReader(url.openStream()));

        String line;

        StringBuilder sb= new StringBuilder();

        while ((line = br.readLine()) != null) {
            if(line.contains("presentationURL")) {
                line = line.split("<presentationURL>")[1].split("</presentationURL>")[0];
            sb.append(line);
            sb.append(System.lineSeparator());
            }
        }
        return sb.toString();

... But when I try to add a second element right after here:

if(line.contains("presentationURL")) {
line = line.split("<presentationURL>")[1].split("</presentationURL>")[0];
sb.append(line);
sb.append(System.lineSeparator());
}

like this:

if(line.contains("manufacturer")) {
line = line.split("<manufacturer>")[1].split("manufacturer")[0];
sb.append(line);
sb.append(System.lineSeparator());
}

the program gives me Array Index Out of Bounds Exception and some other errors. How can I make this program work for more elemets?

5
  • 2
    Do not to use String.split or other low-level string operations to read XML, because it leads to restrictions and errors (as you now already noticed). Instead, use an XML parser. Commented Sep 6, 2018 at 8:56
  • Do you need collect all tags or any of you know? You can see, how do it with XMLStreamReader stackoverflow.com/questions/51836506/… Commented Sep 6, 2018 at 9:11
  • Can I use XML parser for urls? @ThomasFritsch Commented Sep 6, 2018 at 10:07
  • @firestarter yes, i think so. As far as i remember you can read XML from URL, InputStream, File, Reader, ... Commented Sep 6, 2018 at 10:57
  • Agreed with @ThomasFritsch , Use stax pai to read xml from an inputstream . stackoverflow.com/questions/4652299/read-xml-string-using-stax Commented Sep 6, 2018 at 11:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.