[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Parse tag in string

Posted on 2002-04-03
5
Medium Priority
?
189 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 1200 total points
ID: 6917364
Forgot to answear ... :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

656 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