Solved

Parse tag in string

Posted on 2002-04-03
5
179 Views
Last Modified: 2013-11-19
I am looging for a utility to purge a tag from a string.

Example: "Today's date is <DATE FORMAT=MM/DD/YYYY>."

I need an object to store the tag type (Date) and the different properties and their values.

Can anyone help me?

Thanks!!!
0
Comment
Question by:afpcos
5 Comments
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6915417

Well, can't you use java.text formats?
0
 
LVL 1

Expert Comment

by:rwinkler
ID: 6915789
I don't know how closely you're expecting your data to be like XML, but JDOM might be a good place to look.  http://www.jdom.org
0
 

Expert Comment

by:rashidkamranbs
ID: 6915986
Well not 100% but StreamTokenizer could be a helpful utility to purify the date tag from the string..

0
 
LVL 9

Expert Comment

by:Ovi
ID: 6917362
Here is a variant of your parser.

import java.util.*;

public class Tester {
  private String text;
  Vector tags;

  public Tester(String txt) {
    text = txt;
    tags = new Vector();
  }

  public void setText(String txt) {
    text = txt;
    tags = new Vector();
  }

  public void parse() {
    int idxBegin = -1;
    while((idxBegin = text.indexOf("<", idxBegin + 1)) != -1) {
      int idxEnd = text.indexOf(">", idxBegin);
      if(idxEnd != -1) {
        Tag tag = parseTag(text.substring(idxBegin + 1, idxEnd));
     if(tag != null)
       tags.addElement(tag);
      } else return;
    }
  }

  public Tag parseTag(String tagText) {
    Tag tag = new Tag();
    int spaceIdx = tagText.indexOf(" ");
    if(spaceIdx != -1) {
      tag.name = tagText.substring(0, spaceIdx);
      tagText = tagText.substring(spaceIdx + 1);
      int eqIdx = -1;
      while((eqIdx = tagText.indexOf("=")) != -1) {
        String attr = tagText.substring(0, eqIdx);    
     tagText = tagText.substring(eqIdx + 1).trim();
     int nextEq = tagText.indexOf("=");
     if(nextEq == -1) {
       tag.attributes.put(attr, tagText);
       break;
     } else {
       String tmp = tagText.substring(0, nextEq);
       spaceIdx = tmp.lastIndexOf(" ");
       if(spaceIdx != -1) {
         String attrValue = tagText.substring(0, spaceIdx);
         tag.attributes.put(attr, attrValue);
         tagText = tagText.substring(spaceIdx).trim();
       } else {
         tag.attributes.put(attr, tmp);
       }
     }
      }
      return(tag);
    }
    return(null);
  }
 
  public void dump() {
    System.out.println("Text : " + text);
    for(int i = 0; i<tags.size(); i++) {
      ((Tag) tags.elementAt(i)).dump();
    }
  }

  class Tag {
    public String name;
    public Hashtable attributes;

    public Tag() {
      name = "";
      attributes = new Hashtable();
    }
   
    public void dump() {
      System.out.println("******************Tag *************");
      System.out.println("Name : " + name);
      System.out.println("Attributes : ");
      Enumeration attrs = attributes.keys();
      while(attrs.hasMoreElements()) {
        String key = attrs.nextElement().toString();
     String value = attributes.get(key).toString();
     System.out.println("Attribute : " + key + "; Value : " + value);
      }
      System.out.println("***********************************");
    }
  }

  public static void main(String[] args) {
    Tester t = new Tester("Today's date is <DATE FORMAT=MM/DD/YYYY>");
    t.parse();
    t.dump();
    t.setText("Today's date is <DATE FORMAT=MM/DD/YYYY>; tomorrow will be <DATE FORMAT=DD/MM/YY VALUE=04/04/02>");
    t.parse();
    t.dump();
    t.setText("Today  <PERSON NAME=aName AGE=70 SEX=M>; tomorrow will be  <PERSON NAME=bName AGE=90 SEX=F>");
    t.parse();
    t.dump();
  }
}
0
 
LVL 9

Accepted Solution

by:
Ovi earned 300 total points
ID: 6917364
Forgot to answear ... :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now