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 ver开发者_如何学Csion="1.0" encoding="UTF-8">
<validateemail>
<emailid>xyz@gmail.com</emailid>
<address>abc,street</address>
</validateemail>
After that formed XML is return as String.please guide me.
Thanks
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
Perhaps you should go through some tutorial related to this. This is the first one that I found in google search.
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.
I'm supposing the final xml to look like this:
<?xml version="1.0" encoding="UTF-8"> 
<validateemail> 
 <emailid>xyz@gmail.com</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.
You can use Java DOM API, it's the easiest way. "Link"
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("admin@admin.com", "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());
}
}
'
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论