Merge two XML files From java
hi,
i'm in need to merge two XML files from my java code please help me as soon......
Rajesh42 wrote:
hi,
i'm in need to merge two XML files from my java code please help me as soon......Hello Rajesh42,
While it is wonderful that you have discovered the magically code tags, please consider if it is the right time to use them. Code tags should only go round code. Note used to make your posts "stand out", or used to quote other people. Just code.
As to your question, what have you tried, what are you stuck on?
Mike
Similar Messages
-
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 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 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 -
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
AshishMarshall JavaBean to an XML document with JAXB or XMLBeans.
-
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 -
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);
} -
Merge Two Xml files ????
I have two xml files, which I want to merge.
Both are having same structure�
Eg. *INPUT-FILE-1*
<library>
<book isbn="1">
<name>Book-11</name>
<author>steve</author>
<price>2.99</price>
</book>
<book isbn="2">
<name>Book-22</name>
<author>john</author>
<price>12.99</price>
</book>
</library>
*INPUT-FILE-2*
<library>
<book isbn="1">
<name>Book-11</name>
<author>steve</author>
<price>2.99</price>
</book>
<book isbn="3">
<name>Book-33</name>
<author>johnthan</author>
<price>12.99</price>
</book>
</library>
*OUTPUT-file*
<library>
<book isbn="1">
<name>Book-11</name>
<author>steve</author>
<price>2.99</price>
</book>
<book isbn="2">
<name>Book-22</name>
<author>john</author>
<price>12.99</price>
</book>
</book>
<book isbn="3">
<name>Book-33</name>
<author>johnthan</author>
<price>12.99</price>
</book>
</library>Merge should provide third file without any repetition of any record.
Which parser to use and how to use ???
Any help on this ???????????
Thanks !!Merge should provide third file without any repetition of any record.
Which parser to use and how to use ???Look at JDOM
Any help on this ???????????One question mark is all that is necessary. -
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();
} -
Reference faces-config.xml file from java code.
I would like to reference the navigation rules I have set up in my faces-config.xml file from inside my source code.
For example:
Navigation Rule:
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>pricingEngine</from-outcome>
<to-view-id>/faces/template/t_pricing_engine.jsf</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
I would like do some sort of lookup by 'pricingEngine' and get '/faces/template/t_pricing_engine.jsf' back.
Any ideas?I would like to reference the navigation rules I have set up in my faces-config.xml file from inside my source code.
For example:
Navigation Rule:
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>pricingEngine</from-outcome>
<to-view-id>/faces/template/t_pricing_engine.jsf</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
I would like do some sort of lookup by 'pricingEngine' and get '/faces/template/t_pricing_engine.jsf' back.
Any ideas? -
Merging two xml files into single xml file
i hav to merge 2 xml files in to single xml file
my xml files are
input1.xml
<?xml version="1.0"?>
<PreVCD>
<component name="stack">
<subpath path="stack_environment">
<variable var="ins" symbol="!" wireonbus="1"/>
</subpath>
</component>
<dump>
<time t="0">
<data>
<symbol sign="!" value="0"/>
</data>
</time>
<time t="10">
<data>
<symbol sign="!" value="1"/>
</data>
</time>
<time t="25">
<data>
<symbol sign="!" value="0"/>
</data>
</time>
</dump>
</PreVCD>
input2.xml
<?xml version="1.0"?>
<PreVCD>
<component name="stack">
<subpath path="stack_behavior">
<variable var="i" symbol="@" bussize="1"/>
</subpath>
</component>
<dump>
<time t="0">
<data>
<symbol sign="@" value="0"/>
</data>
</time>
<time t="5">
<data>
<symbol sign="@" value="1"/>
</data>
</time>
<time t="10">
<data>
<symbol sign="@" value="0"/>
</data>
</time>
<time t="20">
<data>
<symbol sign="@" value="1"/>
</data>
</time>
</dump>
</PreVCD>
The ouput should look like:
<PreVCD>
<component name="stack">
<subpath path="stack_behavior">
<variable var="i" symbol="@" bussize="1"/>
</subpath>
<subpath path="stack_environment">
<variable var="ins" symbol="!" wireonbus="1"/>
</subpath>
</component>
<dump>
<time t="0">
<data>
<symbol sign="@" value="0"/>
<symbol sign="!" value="0"/>
</data>
</time>
<time t="5">
<data>
<symbol sign="@" value="1"/>
</data>
</time>
<time t="10">
<data>
<symbol sign="@" value="0"/>
<symbol sign="!" value="1"/>
</data>
</time>
<time t="20">
<data>
<symbol sign="@" value="1"/>
</data>
</time>
<time t="25">
<data>
<symbol sign="!" value="0"/>
</data>
</time>
</dump>
</PreVCD>
thanks for any adviceMerge xml documents with the xslt document() function.
<?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="/">
<xsl:copy-of select="*"/>
<xsl:copy-of select="document('input2.xml')"/>
</xsl:template>
</xsl:stylesheet> -
How to read XML files from java
i need a sugession that how to read a xml file using java code
and i need to parse using some parsers and display attributes and entity seperately
as a string.......import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.Iterator;
import java.util.StringTokenizer;
public class XmlParser
private String Result="";
private String Final="";
private String Delim="";
public void bar1(Document document) throws DocumentException
org.dom4j.Element root = document.getRootElement();
// System.out.println(root.getName());
bar2(root);
System.out.println(this.Result);
process();
public void bar2(org.dom4j.Element e)
for(Iterator i = e.elementIterator();i.hasNext();)
org.dom4j.Element Element = (org.dom4j.Element) i.next();
Result += Element.getName()+"\t"+Element.getText()+"\n";
bar2(Element);
public void process()
StringTokenizer Tokenizer = new StringTokenizer(this.Result,"\n");
String element;
while(Tokenizer.hasMoreTokens())
element = Tokenizer.nextToken();
StringTokenizer Tokenizer2 = new StringTokenizer(element,"\t");
// Do what ever String Process here Example
this.Final += element.getName();
this.Final += this.Delim;
System.out.println(this.Final);
public static void main(String s[])throws Exception
Document document = null;
SAXReader reader = new SAXReader();
File f1= new File("D:/Rajesh/EDI to XML/EDI.xml");
document = reader.read(f1);
Demo obj = new Demo();
obj.bar1(document);
i think this will hep full....... -
Creating an xml file from java.
I trying to create an xml file using a java program. I just wondering what is the best way to go about it and what should i use jdom ,xerces sax etc.
Use JAXP+SAX.
Here an example:import java.io.*;
// SAX classes.
import org.xml.sax.*;
import org.xml.sax.helpers.*;
//JAXP 1.1
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.*;
// PrintWriter from a Servlet
PrintWriter out = response.getWriter();
StreamResult streamResult = new StreamResult(out);
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
// SAX2.0 ContentHandler.
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");
serializer.setOutputProperty(OutputKeys.INDENT,"yes");
hd.setResult(streamResult);
hd.startDocument();
AttributesImpl atts = new AttributesImpl();
// USERS tag.
hd.startElement("","","USERS",atts);
// USER tags.
String[] id = {"PWD122","MX787","A4Q45"};
String[] type = {"customer","manager","employee"};
String[] desc = {"Tim@Home","Jack&Moud","John D'o�"};
for (int i=0;i<id.length;i++)
atts.clear();
atts.addAttribute("","","ID","CDATA",id);
atts.addAttribute("","","TYPE","CDATA",type[i]);
hd.startElement("","","USER",atts);
hd.characters(desc[i].toCharArray(),0,desc[i].length());
hd.endElement("","","USER");
hd.endElement("","","USERS");
hd.endDocument();
[i]This xml generation program might be the best solution because it uses JAXP 1.1 so it will work under JDK 1.4 or JDK 1.2/1.3 with XALAN2 library (or any XML library JAXP 1.1 compliant). It's also memory-friendly because it doesn't need DOM.
See http://www.javazoom.net/services/newsletter/xmlgeneration.html
Hope That Helps -
Reading XML file from java web service
Hi
I am developing web service using Netbeans and the application server is glassfish.
but I am facing a problem
I have folder called "config" which will include the xml files. What I need to do setting path into that folder
but it will read from another location when I am deploying it
How to set our own path ?
Error
DPL5306:Servlet Web Service Endpoint [NewWebService] listening at address [http://kapila.epic.lk:8080/WebApplication2/NewWebServiceService]
deployed with moduleid = WebApplication2
java.io.FileNotFoundException: /root/MyWorks/glassfish-v2/domains/domain1/config/OSconfig/MConfig.xml (No such file or directory)
at java.io.FileInputStream.open(Native Method)
My "conifg" folder, i have put in to working directory but it will read from "/root/MyWorks/glassfish-v2/domains/domain1/config/OSconfig/MConfig.xm"
below method is used for getting its contain value
public String getMainCofig() {
String v ="";
try{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("OSconfig/MConfig.xml"));
doc.getDocumentElement().normalize();
NodeList listOfSPRMs = doc.getElementsByTagName("MainConfig");
Node firstSPRMNode = listOfSPRMs.item(0);
if (firstSPRMNode.getNodeType() == Node.ELEMENT_NODE) {
Element firstSPRAMElement = (Element) firstSPRMNode;
// Getting platform
NodeList server_port = firstSPRAMElement
.getElementsByTagName("RunningPlatform");
Element server_port_el = (Element) server_port.item(0);
NodeList server_port_List = server_port_el.getChildNodes();
v= ((Node) server_port_List.item(0))
.getNodeValue().trim();
}catch (Exception e){
e.printStackTrace();
return v;
Regards
I didn't quite get what the problem is but if the the desired file is in custom directory why don't you just use absolute path to reference it?
Like: "/home/my/OSconfig/MConfig.xml" -
How to Read and Generate XML file from java code.
hi guys,
how to read the xml file (Condition :we know only DTD or Shema only).
How to Generate the new xml file ?(using Shema )
And one more how directly Generate the xml from DB?
Pleas with code or any URLUsing XMLbeans you can generate Java objects from an XSD schema (perhaps DTDs aswell)
Then you can create an instance of the Document object and ask it to write itself.
This will create an XML document complient to the schema.
XMLBeans generates a "type" safe DOM where you can only ever have a structure compilent to you schema.
matfud
Maybe you are looking for
-
Each year for the past 5 years I make a family calendar and send copies to all he family members around the globe. I hate that I have to recreate all the birthdays and special occasions from scratch, and re-drag all the photos onto these dates, in t
-
CRS 10.0.2 Error
Hi I have installed CRS 10.0.3 , set up was successful without error, But when I am starting server, getting errors. Environment : Weblogic 10.3.5 ATG 10.0.3 JDK 1.6.0_24 SQL server 2008 / MySql 5 (tried both ) log : Thu Dec 22 09:23:16 IST 2011 1324
-
Why does my leading zero disappear
I have created a 4 number field. Why does my leading zero disappear when moving onto the next field. How can I correct this? Thanks.
-
How do I get an additional text on rollover?
Hi guys, So I'm looking to get an extra text to show when hovering over a thumbnail. Situation: I have a thumbnail linking to a page (frame in timeline) with a YouTube video on it. So I want the title of the video to show up under the thumbnail upon
-
IPhone in Europe/roaming/wifi
So as mentioned in an earlier post, I will be in Europe as of the end of this month for a three month + visit. If I actually needed an excuse to do so one of the main reasons for buying an iPhone was to use it over there, ( UK and France ), during th