Reputation: 9384
i am new for XML.i have a function that takes four input.on the basis of these four parameter create an XML using Java.for example:
<?xml version="1.0" encoding="UTF-8">
<validateemail>
<emailid>[email protected]</emailid>
<address>abc,street</address>
</validateemail>
After that formed XML is return as String.please guide me.
Thanks
Upvotes: 0
Views: 336
Reputation:
Here's an example how I did this, Tell me if it's help :) ?
' ValidateEmail entitie
public class ValidateEmail {
private String emailId;
private String address;
public ValidateEmail(){}
public ValidateEmail(String emailId, String address) {
this.emailId = emailId;
this.address = address;
}
//Getters / Setters
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
**MyXMLMaker**
package com.isi.lf.myXMLMaker;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.isi.lf.entities.ValidateEmail;
public final class myXMLMaker {
public static Document getXMLDocumentFromValidateEmail(ValidateEmail ve){
Document doc = null;
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
doc.setXmlVersion("1.0");
doc.setXmlStandalone(true);
//Generate the XML doc
Element root = doc.createElement("validateemail");
Element emailid = doc.createElement("emailid");
emailid.setTextContent(ve.getEmailId());
root.appendChild(emailid);
Element address = doc.createElement("address");
address.setTextContent(ve.getAddress());
root.appendChild(address);
doc.appendChild(root);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
}
**The main for the output test**
package com.isi.lf.myMain;
import org.w3c.dom.Document;
import com.isi.lf.entities.ValidateEmail;
import com.isi.lf.myXMLMaker.myXMLMaker;
public class MyMain {
/**
* @param args
*/
public static void main(String[] args) {
Document doc = myXMLMaker.getXMLDocumentFromValidateEmail(new ValidateEmail("[email protected]", "Montréal Canada"));
System.out.println(doc.getChildNodes().item(0).getChildNodes().item(0).getNodeName()+" : "+doc.getChildNodes().item(0).getChildNodes().item(0).getTextContent());
System.out.println(doc.getChildNodes().item(0).getChildNodes().item(1).getNodeName()+" : "+doc.getChildNodes().item(0).getChildNodes().item(1).getTextContent());
}
}
'
Upvotes: 0
Reputation: 348
I'm supposing the final xml to look like this:
<?xml version="1.0" encoding="UTF-8">
<validateemail>
<emailid>[email protected]</emailid>
<address>abc,street</address>
</validateemail>
instead of directing you towards the APIs, here is something to just get you started: you would have to use StringBuilder object.
StringBuilder sb = new StringBuilder();
sb.AppendLine("<?xml version="1.0" encoding="UTF-8">");
then add the parameters accordingly,
sb.AppendLine("<validateemail>");
sb.AppendLine("<emailid>"+emailidvalue+"</emailid>");
The same can be done for other parameters as well. This is only a rough idea for the problem. how you implement it is strictly upto you.
Upvotes: 0
Reputation: 1499840
The built-in XML APIs in Java can be a bit of a pain. You may want to use something like JDOM instead (or any of the many other APIs available). There are various tutorials available, including this one which covers quite a bit of the API simply.
Upvotes: 1
Reputation: 1334
There are different ways of generating XML: DOM, SAX, JAXP. I prefer DOM over e'thing because of its' simplicity. You can try this link: http://genedavis.com/library/xml/java_dom_xml_creation.jsp
Upvotes: 1