1

I am having a real problem with parsing and I dont know how to deal with it. Im working on a database that has a very untypical return of information . When you make a request the response is in plain text and I have no idea how to parse it.

The result of a query looks like this:

error=0; 
---
name= Pen
detailname= TextMarker Highlighter Pink
---

There is no html, XML or json involved - The result litterally comes back like this.

I have no idea on how to parse plain text in Java. How do I parse for example, so I only get the information following name= or any other of the categories? I have tried using JSoup, but I cant figure it out. Could anyone with experience shed some light on how I can filter this?

Thank you
Paul

2 Answers 2

1

Since it looks like a *.properties-file I would suggest

Properties p = new Properties();
p.load(inputStream);

You just have to get your results as an InputStream or Reader (i.e. StringReader) and can then access it like Properties.

String error = p.getProperty("error");
String name = p.getProperty("name");
String detailName = p.getProperty("detailname");

which will get you:

error: "0;"
name: " Pen"
detailName: " TextMarker Highlighter Pink"

The only thing you have to keep in mind is that "---" will be an entry in the properties as well.

Newlines in the values may break this.

Sign up to request clarification or add additional context in comments.

1 Comment

I owe you a beer! Thank you a million times!! This seems to have done the trick, but I have to check when I get home to!
0

Not sure if it works on Android but in Java, can't you use a Scanner to read in the text? e.g. something along the lines of

Scanner scanner = new Scanner(new FileReader(fFile));
while ( scanner.hasNextLine() )
{
     String content = scanner.next();
     ....

     // check text for whatever values you need, e.g.
     if(content.startsWith("name=")
     {
            // do your thing
            ....
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.