Results 1 to 6 of 6
  1. #1
    peliukasss is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default Help with html tags in java

    Hello everybody,

    I'm writing a program in java and I have a problem. I want to read a string from tags. Like this: <body> Some text </body>. Could anyone have an idea how to solve it?

    Thanks.

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Take a look here for a free parsers:

    Open Source HTML Parsers in Java

    I cannot know which one will fulfill your needs,
    but important thing is that parser is smart enough to clean
    all non-tag content, so you can easily get what's inside of tags.

    good luck

  3. #3
    peliukasss is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default

    for example:
    <body>
    text text
    word1 word2
    sentence text word
    </body>
    if i have text like this between tags the result is: "text text word1 word2 sentence text word" if i use stringtokenizer I get
    text
    text
    word1
    word2
    sentence
    text
    word

    what i need is that when read that text between tags and I want to get it as it was before. The result I need is:
    text text
    word1 word2
    sentence text word

    I tried using parser, but it does not works like i need
    Please help

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by peliukasss View Post
    I tried using parser, but it does not works like i need
    This unfortunately doesn't tell us much.

  5. #5
    peliukasss is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default

    I found a solution
    Here it is, maybe you need it someday:

    public void getTextBetweenBodyTags() throws IOException{
    ArrayList<String> textBetweenBody = new ArrayList<String>();
    int findBody = 0;
    File file = new File(inPutfile);
    BufferedReader reader = null;
    String line;
    try{
    reader = new BufferedReader(new FileReader(file));
    while ((line = reader.readLine()) != null){
    if (line.equals("</body>")){
    findBody = 0;
    }
    if (findBody == 1){
    textBetweenBody.add(line);
    }
    if (line.equals("<body>")){
    findBody = 1;
    }
    }
    } catch (FileNotFoundException e){
    e.printStackTrace();
    }
    }

  6. #6
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Quote Originally Posted by peliukasss View Post
    I found a solution
    Here it is, maybe you need it someday:

    public void getTextBetweenBodyTags() throws IOException{
    ArrayList<String> textBetweenBody = new ArrayList<String>();
    int findBody = 0;
    File file = new File(inPutfile);
    BufferedReader reader = null;
    String line;
    try{
    reader = new BufferedReader(new FileReader(file));
    while ((line = reader.readLine()) != null){
    if (line.equals("</body>")){
    findBody = 0;
    }
    if (findBody == 1){
    textBetweenBody.add(line);
    }
    if (line.equals("<body>")){
    findBody = 1;
    }
    }
    } catch (FileNotFoundException e){
    e.printStackTrace();
    }
    }
    but dont you think this only good when the info is in between the body tags.
    if theres any other tags for font or font size or anything else. it would be not so useful.

Similar Threads

  1. Need help in validation HTML tags
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-09-2008, 05:18 AM
  2. HTML tags anyone?
    By tim in forum Suggestions & Feedback
    Replies: 2
    Last Post: 06-29-2008, 05:49 AM
  3. Html tags within XML- need help
    By iamhappy in forum XML
    Replies: 2
    Last Post: 03-27-2008, 05:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •