How to split a string to a list in this job? Java
I have a String like this.
<body>
Search Results:
<br>
<br>
Member ID: 10149
<br>
Title:
<br>
First Name: Skye
<br>
Middle Name: Susan
<br>
Last Name: Sommers
<br>
Comment: Entry Report completed.
<br>
Time Stamp: 2011-10-13 14:43:36
<br>
Select Agent: Century 21
<br>
Agent Details: Peter Thorpe Century 21 33 Diamond Drive Newcastle NSW 2300
<br>
Street: 49 Fairway Court
<br>
Suburb: Newcastle
<br>
Postcode: 2300
<br>
<br>
Member ID: 10149
<br>
Title:
<br>
First Name: Skye
<br>
Middle Name:
<br>
Last Name: Sommers
<br>
Comment: Qtr inpection showed residence in very good condition. Walls and floors very clean. Back and front yard both neatly kept.
<br>
Time Stamp: 2011-10-13 12:40:31
<br>
Select Agent: Century 21
<br>
Agent Details: Peter Thorpe Century 21 33 Diamond Drive Newcastle NSW 2300
<br>
Street: 49 Fairway开发者_C百科 Court
<br>
Suburb: Newcastle
<br>
Postcode: 2300
How to split String its into a Array with array with first member is:
Member ID: 10149
Title:
First Name: Skye
Middle Name: Susan
Last Name: Sommers
Comment: Entry Report completed.
Time Stamp: 2011-10-13 14:43:36
Select Agent: Century 21
Agent Details: Peter Thorpe Century 21 33 Diamond Drive Newcastle NSW 2300
Street: 49 Fairway Court
Suburb: Newcastle
Postcode: 2300
Thank for helping!
This is difficult to do quickly because of the potential conflicts. You can use String.replace()
to drop the <br>
tags. Then you could do a String.split()
on the word "Member", but then it would break if "Member" shows up anywhere else.
A more controlled, but complex method, would be to split()
on newlines and then read each line, checking for a startsWith()
and then filling in the appropriate data. Considering your result above isn't much more helpful, this would also allow you to actually create a Record
object and fill it in, line by line.
Your order of elements is not changing, however you want to create a List
of of beans.
May I suggest using Apache Commons DynaBeans instead in this situation? They are objects that you can create on the fly and access values by using bean.get("key");
and you can set values by using bean.set("key", "value");
So what you can do is split your html using something like follows:
String html = "<your html>";
List l = new ListArray();
int index = 0;
while((index = html.indexOf("Member", index)) > -1) {
//nextIndex is the end of the first element.
int nextIndex = html.indexOf("<br>\n<br>",index);
String element = html.substring(index, nextIndex);
//parse element string to create dynabeans
DynaBean bean = createDynaBean(element);
l.add(bean);
index = nextIndex;
}
I havne't provide the code for creating a dynabean. take a look here for an example on how to create a dynabean. Since you have a string prepresenting 1 element in your List
it should be easy to use split()
or indexOf()
etc to get the values for the elements you want. I would start by splitting on \n
and then split on :
which would give me key and value for each line.
Hope this helps.
What I would do is this pseudo-code:
Member newMember = null;
List<Member> result = new ArrayList<Member>();
for each line
trim whitespace
// control new objects
if line starts with "Member ID:"
if newMember != null
result.add(newMember);
newMember = new Member()
// add values to the current object
else if newMember != null (at the beginning there's no created object)
if line starts with "field1:"
newMember.setField1(parse value from line)
else if line starts with "field2:"
newMember.setField2(parse value from line)
It should suffice if your input is well formed.
I would do it this way:
// remove spaces and tags
html = html.replaceAll("(?m)(<.*?>|Search Results:|^ *)", "");
// remove empty lines
html = html.replaceAll("(?m)^[ \t]*\r?\n", "");
// split by newlines
String[] results = html.split("\\n")
// use results
You can then split each results[i]
with :
to get key and value.
First split according to<br>\n<br>
and then split the arrays[1] element with <br>
character, then iterate over all splited strings and thake whatever you want.
Here's a one-line solution for chopping it up:
String[] parts = input.split("(?m)\\s*(^.*<.*$)+\\s*");
From the input above, this will give you:
[, Search Results:, , Member ID: 10149, Title:, First Name: Skye]
Iterate over it, ignoring the stuff you don't want. Assuming "Search Results:" marks a new person, do this:
List<Map<String, String>> peopleList = new ArrayList<Map<String, String>>();
Map<String, String> person = null;
for (String part : parts) {
if (!part.contains(":") || person == null) continue;
if (parts.startsWith("Search Results")) {
person = new HashMap<String, String>();
peopleList.add(person);
continue;
}
String[] nameValue = part.trim().split(":");
person.put(nameValue[0].trim(), nameValue[1].trim());
}
Now you have a list of maps, each with entries like "Member ID"="10149" etc
精彩评论