Нужна помощь для выполнения слияния двух одинаковых значений контактов.
Я хотел бы знать, как я могу объединить два одинаковых значения контакта в один контакт, прежде чем преобразовать его в XML-файл с помощью Java.
В моем случае у меня есть такие контакты:
Contact No1:
Contact
Arun
Arun_niit
nuraaa_iceee@yahoo.co.in
Contact
Contact No2:
Contact
Arun
Arun_niit
nuraaa_iceee@gmail.com
Contact
Контакт № 1 и 2 имеет то же имя, а также контакт № 3 и 4
Contact No3:
Contact
Rangarajkarthik
karthik Rangaraj
kart26@gmail.com
karthikranga@yahoo.com
Contact
Contact No4:
Contact
Rangaraj
karthik
kart26@gmail.com
karthikranga@yahoo.com
Contact
Указанный выше контакт повторяется дважды с тем же именем и адресом электронной почты. Как я могу объединить это как один контакт?
Это мой файл .partf, и я хочу преобразовать его в файл XML, что я и сделал. Но я хочу объединить эти контакты и затем создать XML, используя мой JavaCode.
Contact
Arun
Arun_niit
nuraaa_iceee@yahoo.co.in
Contact
Contact
ColomboGiorgia
Giorgia Colombo
giorgiacolombo81239@libero.it
Contact
Contact
Arun
Arun_niit
nuraaa_iceee@gmail.com
Contact
Contact
KumarVeera
Veera Kumar
KUMARg_8111@yahoo.com
Contact
Contact
MarbellaFunkybuddha
Funkybuddha Marbella
http://www.facebook.com/profile.php?id=1123301493096451
Contact
Contact
Rangarajkarthik
karthik Rangaraj
kart2006@gmail.com
karthikrangaraj@yahoo.com
Contact
Contact
Rangaraj
karthik
kart26@gmail.com
karthikranga@yahoo.com
Contact
Мой JavaCode:
package textparser;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.regex.Pattern;
import org.xml.sax.ContentHandler;
import java.io.Serializable;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import com.sun.xml.internal.bind.util.AttributesImpl;
public class Item {
public static void main (String args[]) {
Item.readWrite("D:/Demo/test.part","D:/Demo/juin202.xml");//Read XML and Save as a XML file.
}
public static void readWrite(String fromFile, String toFile)
{
try{
Pattern p = Pattern.compile(".+@.+\\.[a-z]+");//A compiled representation of a regular expression.
BufferedReader in = new BufferedReader(new FileReader(fromFile));
FileOutputStream fos = new FileOutputStream(toFile);
OutputFormat of = new OutputFormat("XML","windows-1250",true);//Codepage Windows-1250 - Character Code Listing for Central Europe languages.
of.setIndent(1);
of.setIndenting(true);//JDOM API:This will set the indent String to use; this is usually a String of empty spaces.
XMLSerializer serializer = new XMLSerializer(fos,of);
ContentHandler hd = serializer.asContentHandler();//The Serializer interface is implemented by a serializer to enable users to: get an org.xml.sax.ContentHandler or a DOMSerializer to provide input to.
hd.startDocument();
AttributesImpl atts = new AttributesImpl();//Construct a new, empty AttributesImpl object.
hd.startElement("","","CONTACTS",atts);//To create root tag <Contacts>
String line = null,tag;
while ((line=in.readLine())!=null) {
if(line.equals("Contact")){
line=in.readLine();
hd.startElement("","","CONTACT",atts);
int i=0;
while(!line.equals("Contact")){
if(i==0)
tag="FirstName";
else if(i==1)
tag="LastName";
else{
if(p.matcher(line).matches())
tag="EMail";
else
tag="URL";
}
hd.startElement("","",tag,atts);
hd.characters(line.toCharArray(),0,line.length());
hd.endElement("","",tag);
i++;
line=in.readLine();
}
hd.endElement("","","CONTACT");
}
}
hd.endElement("","","CONTACTS");
hd.endDocument();
fos.close();
System.out.println("Work is done and check your file specified in the directory");
in.close();
}catch(Exception E){
System.out.println("Cannot Generate XML!!!");
}
}
}
Я делаю что-то не так или есть лучший способ сделать это?