Solved

Parse tag in string

Posted on 2002-04-03
5
186 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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