Creating XML file from Java Bean
Hi
Are there any standard methods in Java 1.5 to create XML file from java bean,
i can use JAXB or castor to do so,
But i would like to know if there is any thing in java core classes,
I have seen XMLEncoder, but this is not what i want.
Any ideas
Ashish
Marshall JavaBean to an XML document with JAXB or XMLBeans.
Similar Messages
-
Hi all i am working to create a XML file using java can any one show me some sample code how to do so
All I suggested was to insert a single line ("X.serialize(root);") into your code. Anyway, here's a ready-to-compile source code based on yours. This code utilizes the Xerces-J class library.import java.io.File;
import java.io.FileWriter;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Element;
public class Test {
public static void main(String[] arguments) {
FileWriter out;
DocumentImpl d;
Element root;
XMLSerializer X;
try {
System.out.println(" creatin ");
File fos = new File("xsr.xml");
out = new FileWriter("xsr.xml");
System.out.println("created File .." + fos.getName());
out.flush();
d = new DocumentImpl();
System.out.println("create root");
root = d.createElement("abc");
System.out.println("creating element");
d.insertBefore(root, null);
//out.write(d.createAttribute(""));
OutputFormat o = new OutputFormat(d);
System.out.println("Output format...");
o.setIndent(5);
o.setIndenting(true);
o.setDoctype("lab1.dtd", "lab1.dtd");
o.setDoctype("name of dtd file", "name of dtd file");
X = new XMLSerializer(o);
X.setOutputCharStream(out);
X.serialize(root);
out.flush();
out.close();
catch (Exception e1){
e1.printStackTrace();
} -
What the best way to create XML files from JAVA application?
Hi to all,
I need to edit and to create new filex in format of xml. I know to parse, but what would be the best way to do:
1. - Create new file, create like simle text file, or there is some clases that know to do it in more simple way
2.a - Edit XML file, to take some data and add it to the XML in the place I want to.
2.b - Also Edit, but not to add fields, just update some of them.
Code examples or links to samples woul be welcomed.
Best regards, Nick.I have tried working with XML directly using the Java classes but this was a pain. I then looked at DOM4J and JDOM and found JDOM easier to use.
-
How I can create a XML file from java Aplication
How I can create a XML file from java Aplication
whith have a the following structure
<users>
<user>
<login>anyName</login>
<password>xxxx</password>
</user>
</users>
the password label must be encripted
accept any suggestionLet us assume you have all the data from the jsp form in an java bean object..
Now you want a xml file. This can be acheived in 2 ways
1. Write it into a file using java.io classes. Say you have a class with name
write("<name>"+obj.getName+</name>);
bingo you have a flat file with the xml
2. Use data binding to do the trick
will recommend JiBx and Castor for the 2nd option
Regards,
Rajagopal -
How to edit the existing data in the XML file from java programming.
Hi all
i am able to create XML file with the sample data as below from java programming.
i need sample code on how to edit the existing data in the XML file?
for example
<?xml version="1.0"?>
<mydata>
<data1>
<key1>467</key1>
<name1>Paul</name1>
<id1>123</id1>
</data1>
<data2>
<key2>467</key2>
<name2>Paul</name2>
<id2>123</id2>
</data2>
</mydata>
i am able to insert the data in the XML.
now i need sample code on how to modify the data in the above XML file from the java programming for only key2,name2,id2 tags only. the remaining tags data in the XML file i want to keep same data except for key2,name2,id2 which are i want to modify from java code
Regards
Sunil
[points will be always rewardable]hi
u need a parser or validate the xml file for to read the xml file from java coding u need for this
xml4j.jar u can download this file from here
http://www.alphaworks.ibm.com/tech/xml4j
or we can use the SAX(simple API for XML)
some sample applications for this
http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html
http://www.developertutorials.com/tutorials/java/read-xml-file-in-java-050611/page1.html
http://www.xml-training-guide.com/e-xml44.html
let me know u need any other info
bvr -
How to modify an existing xml file from java code.
Hi
I have worked on creating a new xml file from java code using xmlbeans.But if i try to modify an already existing file using java code I am unable to get errorfree xmlfile.
For example if xml file(studlist.xml) is as below:
<?xml version="1.0" encoding="UTF-8"?>
<StudentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:\kchaitanya\xmlprac1\abc\Studlist.xsd">
<Student>
<Name>ram</Name>
<Age>27</Age>
</Student>
<Student>
<Name>sham</Name>
<Age>26</Age>
</Student>
</StudentList>
Now suppose i have set name to victor using student.setName,
and set age to 20 using setAge from javacode,
the new xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<StudentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:\kchaitanya\xmlprac1\abc\Studlist.xsd">
<Student>
<Name>ram</Name>
<Age>27</Age>
</Student>
<Student>
<Name>sham</Name>
<Age>26</Age>
</Student>
</StudentList>
<Student>
<Name>victor</Name>
<Age>20</Age>
</Student>
As observed this is not a valid xml file.But how can i modify without any errors?I know it's an old post, but I found this while doing a google search for something else, and don't like to leave it un-aswered
Just in case anyone has a similar problem... In this case the new elements have been appended outside of the root element
What you need to do is first get the root element and then append the new children to that, there are several ways of getting the root element, which depend on what you want to do with the elements you get back here's a simple (incomplete) way.
// gets the root element of the specified file (code not shown)
Element rootElement= new SAXReader().read(file).getRootElement();Then just append the new elements as below (this is non-generic code and would need to be modified for your situation)
// write a new student element
Element student = document.createElement("Student"); // creates the new student
rootElement.appendChild(student); // ***appends it to the root element***
Element name = document.createElement("Name"); // creates the name element
name.appendChild(document.createTextNode("Fred")); // adds the name text to the name element
student.appendChild(name); // appends the name to the student
Element age= document.createElement("Age"); // creates the age element
age.appendChild(document.createTextNode("26")); // adds the age text to the age element
student.appendChild(age); // appends the name to the studentThen flush ya buffers or whatever and write the file
Edited by: Dream-Scourge on Apr 23, 2008 11:10 AM -
Generation of xml file from java code
hi,
I want to manipulate data in a xml file with java code.I have read data from xml file and also changed it. But i am unable to covert it again in xml file from java code. Can you please tell me how i can do this?Let me know which parser are you using currently for reading xml files so that i assist you. For now, you can refer to STAX Parser API under this link
http://java.sun.com/webservices/docs/1.6/tutorial/doc/SJSXP3.html -
How to create xml file from Oracle and sending the same xml file to an url
How to create xml file from Oracle and sending the same xml file to an url
SQL/XML (XMLElement, XMLForest, XMLAgg, etc) and UTL_HTTP.
Whether that works for you with the version of Oracle you have, your requirements, and needs is another story. A little detail goes a long way. -
Create .xml file from a java file
I wanted to know how can we create a .xml file through java with the given data.
For example: book saler.
data given through java are
1)Sl no
2)Book name
3Book type
4)Book price
i want a xml file generated through the java code
can anyone help me out?i have done like this..
but the problem with this is i want to add more than one book details. i am able to add only one book detais..
package name:createxml
main class:GenerateOrderXml.java
CreateXml.java and order.java
package createxml;
import java.util.Vector;
public class GenerateOrderXml {
public static void main(String[] args) {
try{
Order order= new Order();
CreateXml xml=new CreateXml();
Vector orders=new Vector();
order.setBookName("Java_for_beginers");
order.setAuthorName("Balaguruswamy");
order.setISBN("a123");
order.setNo_of_copies("3");
order.setprice("350");
order.setPublisher("Deepak publishers");
orders.add(0,order);
order.setBookName("Java");
order.setAuthorName("Bala");
order.setISBN("a123");
order.setNo_of_copies("3");
order.setprice("350");
order.setPublisher("Deepak ");
orders.add(1,order);
xml.creatxml(orders);
catch(ArrayIndexOutOfBoundsException e)
e.printStackTrace();
package createxml;
import org.jdom.*;
import org.jdom.output.XMLOutputter;
import java.io.IOException;
import java.util.Vector;
* @author divyashree
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
public class CreateXml {
public void creatxml(Vector orders)
try
Element root = new Element("BookSaler");
Order order= new Order();
order=(Order)orders.elementAt(0);
Element BName = new Element("Bookname1");
BName.setText(order.getBookName());
root.addContent(BName);
Element AName = new Element("Authorname");
AName.setText(order.getAuthorName());
BName.addContent(AName);
Element isbn = new Element("ISBN");
isbn.setText(order.getISBN());
BName.addContent(isbn);
Element price = new Element("Price");
price.setText(order.getprice());
BName.addContent(price);
Element publisher= new Element("Publisher");
publisher.setText(order.getPublisher());
BName.addContent(publisher);
order=(Order)orders.elementAt(1);
Element BName1 = new Element("Bookname2");
BName1.setText(order.getBookName());
root.addContent(BName1);
Element AName1 = new Element("Authorname");
BName1.addContent(AName);
Element isbn1 = new Element("ISBN");
isbn.setText(order.getISBN());
Element price1 = new Element("Price");
BName1.addContent(price);
Element publisher1= new Element("Publisher");
BName1.addContent(publisher);
Document doc = new Document(root);
try {
XMLOutputter serializer = new XMLOutputter();
serializer.setIndent(" ");
serializer.setNewlines(true);
serializer.output(doc, System.out);
catch (IOException e)
System.err.println(e);
}catch(ClassCastException e)
e.printStackTrace();
package createxml;
import java.util.Vector;
public class Order {
protected String ISBN="";
protected String BookName="";
protected String AuthorName="";
protected String No_of_copies;
protected String price;
protected String Publisher="";
public String getISBN()
return this.ISBN;
public String getBookName()
return BookName;
public String getAuthorName()
return AuthorName;
public String getNo_of_copies()
return No_of_copies;
public String getprice()
return price;
public String getPublisher()
return Publisher;
public void setISBN(String isbn)
Vector vec_isbn= new Vector();
ISBN=isbn;
public void setBookName(String bookname)
BookName=bookname;
public void setAuthorName(String authorname)
AuthorName=authorname;
public void setNo_of_copies(String no_of_copies)
No_of_copies=no_of_copies;
public void setprice(String Price)
price=Price;
public void setPublisher(String publisher)
Publisher=publisher;
/***************************************************************************/ -
Hello,
can anybody help me with generating an empty tagged XML file from a XSD file. Any sample java code will be highly appreciated.
Thanx in advance.
A .Apache XML Beans presents a way to compile the XSD Schema and generate Java classes for each element defined in the schema. With these interface you can then create new XML instances from the schema. (http://xmlbeans.apache.org/)
-
Create XML file from ABAP with SOAP Details
Hi,
I am new to XML and I am not familiar with JAVA or Web Service. I have searched in SDN and googled for a sample program for creating XML document from ABAP with SOAP details. Unfortunately I couldn't find anything.
I have a requirement for creating an XML file from ABAP with SOAP details. I have the data in the internal table. There is a Schema which the client provided and the file generated from SAP should be validating against that Schema. Schema contains SOAP details like Envelope, Header & Body.
My question is can I generate the XML file using CALL TRANSFORMATION in SAP with the SOAP details?
I have tried to create Transformation (Transaction XSLT_TOOL) in SAP with below code. Also in CALL transformation I am not able to change the encoding to UTF-8. It's always show UTF-16.
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
<xsl:template match="/">
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Header>
<CUNS:HeaderInfo>
<CUNS:InterfaceTypeId>10006</InterfaceTypeId>
<CUNS:BusinessPartnerID>11223344</BusinessPartnerID>
<CUNS:SchemaVersion>1.0</SchemaVersion>
<CUNS:DateTime>sy-datum</DateTime>
</CUNS:HeaderInfo>
</SOAP:Header>
<SOAP:Body>
<xsl:copy-of select="*"/>
</SOAP:Body>
</SOAP:Envelope>
</xsl:template>
</xsl:transform>
In ABAP program, I have written below code for calling above Transformation.
call transformation ('Z_ID')
source tab = im_t_output[]
result xml xml_out.
call function 'SCMS_STRING_TO_FTEXT'
exporting
text = xml_out
tables
ftext_tab = ex_t_xml_data.
Please help me how to generate XML file with SOAP details from ABAP. If anybody have a sample program, please share with me.
Is there any easy way to create the XML file in CALL Transformation. Please help.
ThanksTry ABAP forum, as it seems not to be PI related.
-
How to Generate XML File from Java Code.
I want to generate the xml file from the java code.
Could you plz suggest any webSite address with example?Here is the code
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreateXML {
private DocumentBuilderFactory factory = null;
private DocumentBuilder builder = null;
private Document document = null;
public CreateXML() {
try {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
document = builder.newDocument();
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
/** Creates the document for xml. */
public Document createDocument(){
try{
Element root = document.createElement("Root");
Element child = document.createElement("child");
root.appendChild(child);
document.appendChild(root);
}catch(RuntimeException e){
e.printStackTrace();
return document;
/** Saves the document as xml. */
public void saveDocument(Document document){
try{
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult stream = new StreamResult(new File("sample.xml"));
transformer.transform(source, stream);
System.out.println("XML Created !!");
}catch(TransformerConfigurationException e){
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
public static void main(String args[]){
CreateXML createXML = new CreateXML();
Document document = createXML.createDocument();
createXML.saveDocument(document);
} -
How to create xml file from original
I have this complicated xml and I want to search through it and make a new xml with only element I find that match. what is the best way to do this? the file
is about 6mb and I want to read the whole file easily..should i load to database table and create a file from table? I have found issues reading the file..
- <ArrayOfJobClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <JobClass>
- <Triggers>
- <TriggerClass>
<ExpireActionType>Delete</ExpireActionType>
<ExpireType>DateTime</ExpireType>
<TriggerType>TimeType</TriggerType>
- <TTime>
<TimeTriggerType>Custom</TimeTriggerType>
<IntervalType>Daily</IntervalType>
<SpecificType>Days</SpecificType>
<FirstLastType>First</FirstLastType>
<IntervalValue>1</IntervalValue>
<FirstLastWeekDay>Monday</FirstLastWeekDay>
<InitDate>2009-05-04T14:17:05.40625-07:00</InitDate>
- <IntervalDays>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>You can load it in the database in an XMLType table (Object-Relational or Binary XML storage), use XQuery on the XML content and then write back the result to a file.
Or, you might just use an external processor (XSLT or XQuery).
If you want to stay in the Oracle world, you can use :
- For XSLT : $ORACLE_HOME/bin/oraxsl (it's a wrapper for the java XSLT engine)
- For XQuery : the java XQuery API
Personally, I sometimes use the Saxon XSLT and XQuery processor, quite efficient.
Here's a simplistic example with oraxsl utility :
emp.xml
<?xml version="1.0" encoding="UTF-8"?>
<emps>
<emp id="7369">
<name>SMITH</name>
<job>CLERK</job>
<salary>800</salary>
</emp>
<emp id="7499">
<name>ALLEN</name>
<job>SALESMAN</job>
<salary>1600</salary>
</emp>
<emp id="7521">
<name>WARD</name>
<job>SALESMAN</job>
<salary>1250</salary>
</emp>
<emp id="7566">
<name>JONES</name>
<job>MANAGER</job>
<salary>2975</salary>
</emp>
<emp id="7654">
<name>MARTIN</name>
<job>SALESMAN</job>
<salary>1250</salary>
</emp>
</emps>
emp.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="text()"/>
<xsl:template match="emps/emp[job='SALESMAN']">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>Applying the transformation to search and extract all "salesmen" :
D:\ORACLE\test>%ORACLE_HOME%\bin\oraxsl emp.xml emp.xsl result.xml
D:\ORACLE\test>type result.xml
<?xml version = '1.0' encoding = 'UTF-8'?>
<emp id="7499">
<name>ALLEN</name>
<job>SALESMAN</job>
<salary>1600</salary>
</emp><emp id="7521">
<name>WARD</name>
<job>SALESMAN</job>
<salary>1250</salary>
</emp><emp id="7654">
<name>MARTIN</name>
<job>SALESMAN</job>
<salary>1250</salary>
</emp> -
Create XML file from table data
Dear All,
with dataservice 4.0, I want to create an XML file from a table data.
Table have a single column but more record, for example:
0001000488;100;EUR;
0001000489;200;EUR;
0001000450;300;EUR;
My desired XML output:
<Data>
0001000488;100;GBP;
0001000489;200;EUR;
0001000450;300;EUR;
</Data>
I try with a sample query but the sistem write only the last record in XML file:
<Data>
0001000450;300;EUR;
</Data>
Can everyone help me?
Thank in advance.
SimoneHello
That is a very simple (also odd) XML document structure, and as such doesn't require use of the XML target. It can be easily acheived by writing a normal file with a header and footer, which is acheived using a row_generation and a query to generate the hard coded open and close tags.
Michael -
Create XML File from a specified XSD file
Hi,
I'd like to create an XML document with java. BUT How can we "bind" this creation with a XSD file. Hence, the creation may fail if the XSD binded file is not respected.
So I know how to create XML file but not bind to this creation my proper XSD file (XML schema). With which tool can I do this ?
Thanks.Hi,
I'd like to create an XML document with java. BUT How can we "bind" this creation with a XSD file. Hence, the creation may fail if the XSD binded file is not respected.
So I know how to create XML file but not bind to this creation my proper XSD file (XML schema). With which tool can I do this ?
Thanks.
Maybe you are looking for
-
Advertise configuration globally using Bonjour
What does this mean "advertise configuration globally using Bonjour" it can be found in the (AE) setup under Airport Tabs- Base Station. If you place a check mark in that field what happens or I say what can happen. Also I have a mac mini that seems
-
Is it possible to deploy a portlet war file on web container?
Hi All, Is it possible to deploy a portlet war file on web container i.e say I have a JSF or struts portlet application deployed on a portal container which I need to run as a web application. So, I would need to deploy the application on web contain
-
Can't adjust framebuffer resolution [SOLVED]
Ever since the last kernel update, I am unable to adjust the framebuffer. I had it set to 1024x768 in the menu.lst file but ever since the update it can only use the default resolution. I've tried both 1024x768 and 1280x1024 resolutions with no luck.
-
Ni 6602 Card, Configuration DIO and Counter in Register Level Programming
Hello, I want to do register level programming with the NI 6602 card . Which register determine a channel act as DIO or Counter.
-
Retrive records based on condition
HI, Need some clarification on getting the records based on the condition given. Here is my requirement. I have developed a function module to search the records based on some input given. For suppose i get some around 10000 records for this conditio