Getting strings from a html doc, storing it in an array JAVA

Im new to Java but have some experience with other programming languages. I know what I want to do but am having trouble with Java structures.

Basically i have a html doc that has references to certain resource files used on a UCM.

All i need to do is take these names as a string from the document, store them in an array and then print them out in ascending order.

The resource names are always on one line and occur like this <@dynamichtml [email protected]>, all i need is the resource_name part.

Below i have a code im having trouble with which reads the html doc in via a buffer and I am trying to implement a throw/catch method but not having much luck.

Please can anyone help?

[CODE]
import java.util.regex.Pattern;


public class Find{
BufferedReader br = null;
ArrayList arrayList = new ArrayList();
try{
br = new BufferedReader(new FileReader("C:Documents and SettingsKieren McDonaldDesktopNickJavamy.html"));
String line = null;
ArrayList arrayList = new ArrayList();
while ((line = br.readLine) != null) {
return m.find()?line.substring(m.start(1), m.end(1)):null;
listA.add(".*");
}
} catch(noInstance) {

ex.printStackTrace();


}
finally {
try{
if (br != null) {
}
}
catch(br.close();

public static String find(String beg, String end, String line) {

Pattern p=Pattern.compile(beg+"(.*)"+end);
Matcher m= p.matcher(line);

return m.find()?line.substring(m.start(1), m.end(1)):null;

}

public static void main(String[] args) {

}



}









[/CODE]

Comments

  • Hey,
    The following code compiles and prints the html file. Note, you'll have to fix the path to the html file to match your file location.

    Logic to make the program do what you want it to do remains to be done.

    [code]import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class Find {

    BufferedReader br = null;
    ArrayList arrayList = new ArrayList();

    public Find() throws java.io.IOException {
    try {
    br = new BufferedReader(new FileReader("C:/Temp/Downloads/my.html"));
    String line = null;
    while ((line = br.readLine()) != null) {
    System.out.println(line);
    // m.find() ? line.substring(m.start(1), m.end(1)) : null;
    // listA.add(".*");
    }
    } catch (FileNotFoundException ex) {
    ex.printStackTrace();
    } finally {
    br.close();
    }
    }

    public static String find(String beg, String end, String line) {

    Pattern p = Pattern.compile(beg + "(.*)" + end);
    Matcher m = p.matcher(line);

    return m.find() ? line.substring(m.start(1), m.end(1)) : null;

    }

    public static void main(String[] args) throws IOException {
    Find findResources = new Find();
    }
    }[/code]

    regards, se52
  • Thanks so much for your help se52 i really appreciate it. If I could give you a gold star or a hi-5 i most definately would :)
  • We Chinese place a high value on responsibility. It is traditional deal for us Chinese to take care of our parents. It is right, then, when they are getting older with each passing day, we help them and see that they are well cared for. In this way, we give back some of the love and care they have given to us.

    Yushu of Qinghai province had taken place earthquake in 2010. There are some people had been dead. As Chinese citizens, we must have a strong sense of responsibility to help them. An disabled old man donated one thousand to disaster area. One child gave a new pair of [link=http://www.onpuma.com/]puma shoes [/link]which bought only three days to a child of yushu. This pair of mens puma shoes spent four hundred. The money was saved three month from pocket money. Just a few days,our country raised money six billion.

    Our army played an important role in this earthquake. They saved a lot of people. We had a grateful to them. Our country is united. To be a responsible citizen ,we must put our country
  • Hi guys, I've almost finished my program, so far I've captured and read in the html document and extracted the data that I wanted before placing it in an array reading for sorting. I then ouputted the contents of that array in an orderly fashion.

    I now would like to write the contents of that array using a method calling the PrintWriter but am having a bit of trouble. Below is the code I have so far.

    P.S. apologies, I dont know why that guy inserted that random post on my page.

    [CODE]
    /**import java librarys*/
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.Collections;
    import java.util.List;
    import java.io.PrintWriter;


    /** class to find string */
    public class Find {
    BufferedReader br = null;

    /** method to read html file line by line and return string to tore in array */
    public Find() throws java.io.IOException {
    try {
    /** read in file */
    br = new BufferedReader(
    new FileReader(
    "C:/Documents and Settings/Kieren McDonald/Desktop/Nick/Java/timelinemanager_resource.htm"));
    /**
    * declaring string identifiers for beginning and end of string
    * aswel as one other line string to store the data between
    */
    String resourceline = null;
    String beg = "<@dynamichtml ";
    String end = "@>;";
    ArrayList arrayList = new ArrayList();
    /** while line is not equal to null then find 3 string values */
    while ((resourceline = br.readLine()) != null) {
    resourceline = find(beg, end, resourceline);

    /**
    * if line is not equal to null store line in array list adding
    * a resource name
    */
    if (resourceline != null) {
    resourceline= resourceline.trim();
    arrayList.add(resourceline);
    }

    }
    sortAndPrint(arrayList);
    } catch (FileNotFoundException ex) {
    ex.printStackTrace();
    } finally {
    br.close();
    }
    }

    /** method to sort and print the array collection */
    private void sortAndPrint(List results) {
    Collections.sort(results,String.CASE_INSENSITIVE_ORDER);
    for (String resourceline : results)
    System.out.println(resourceline);
    }

    /** method to find the value between the beginning and end of a string */
    public String find(String beg, String end, String resourceline) {
    /** match pattern to store string between strings and match to line */
    Pattern p = Pattern.compile(beg + "(.*)" + end);
    Matcher m = p.matcher(resourceline);
    return m.find() ? resourceline.substring(m.start(1), m.end(1)) : null;
    }
    /** method to create a new file to print arrayList collection.*/
    private void writeResults(arrayList arrayList, String ResourceList.txt){



    }

    /** main method to find the resource name calling */
    public static void main(String[] args) throws IOException {
    Find findResources = new Find();
    }


    }
    [/CODE]
  • Hi guys, I've almost finished my program, so far I've captured and read in the html document and extracted the data that I wanted before placing it in an array reading for sorting. I then ouputted the contents of that array in an orderly fashion.

    I now would like to write the contents of that array using a method calling the PrintWriter but am having a bit of trouble. Below is the code I have so far.

    P.S. apologies, I dont know why that guy inserted that random post on my page.

    [CODE]
    /**import java librarys*/
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.Collections;
    import java.util.List;
    import java.io.PrintWriter;


    /** class to find string */
    public class Find {
    BufferedReader br = null;

    /** method to read html file line by line and return string to tore in array */
    public Find() throws java.io.IOException {
    try {
    /** read in file */
    br = new BufferedReader(
    new FileReader(
    "C:/Documents and Settings/Kieren McDonald/Desktop/Nick/Java/timelinemanager_resource.htm"));
    /**
    * declaring string identifiers for beginning and end of string
    * aswel as one other line string to store the data between
    */
    String resourceline = null;
    String beg = "<@dynamichtml ";
    String end = "@>;";
    ArrayList arrayList = new ArrayList();
    /** while line is not equal to null then find 3 string values */
    while ((resourceline = br.readLine()) != null) {
    resourceline = find(beg, end, resourceline);

    /**
    * if line is not equal to null store line in array list adding
    * a resource name
    */
    if (resourceline != null) {
    resourceline= resourceline.trim();
    arrayList.add(resourceline);
    }

    }
    sortAndPrint(arrayList);
    } catch (FileNotFoundException ex) {
    ex.printStackTrace();
    } finally {
    br.close();
    }
    }

    /** method to sort and print the array collection */
    private void sortAndPrint(List results) {
    Collections.sort(results,String.CASE_INSENSITIVE_ORDER);
    for (String resourceline : results)
    System.out.println(resourceline);
    }

    /** method to find the value between the beginning and end of a string */
    public String find(String beg, String end, String resourceline) {
    /** match pattern to store string between strings and match to line */
    Pattern p = Pattern.compile(beg + "(.*)" + end);
    Matcher m = p.matcher(resourceline);
    return m.find() ? resourceline.substring(m.start(1), m.end(1)) : null;
    }
    /** method to create a new file to print arrayList collection.*/
    private void writeResults(arrayList arrayList, String ResourceList.txt){



    }

    /** main method to find the resource name calling */
    public static void main(String[] args) throws IOException {
    Find findResources = new Find();
    }


    }
    [/CODE]
  • This post has been deleted.
  • Hi everybody,

    I uploaded one file. But It can not display. I used internet explorer 8.0.

    How can I repair? Or some problems happened to IE8?

    Rgs

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories