Solved

Parse tag in string

Posted on 2002-04-03
5
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

756 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