Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Frits Walraven
  • Himai Minh
  • Jj Roberts


Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I have some queries related to one task i am doing in our code

I will demonstarte with some pseudo xml files as it will not be possible for me to paste exact code fragment

Lets say i have A.xml
<?xml version......?>
<c>Some data</c>
<c>Another data</c>

<x>hi to all</x>

Now what i want to do is extract all the tags with <B> ffrom this xml(A.xml)& want to append in another xml Say B.xml as a child of root tag say note
something like this
B.xml (Before OPeration)
<?xml ver...........?>


I need this kind of B.xml after processing
B.xml (after OPeration )
<?xml ver...........?>
<c>Some additionaldata</c>
<c>Another additional data</c>


I am using XPath for finding out all B's element from A.xml
with expression like /a/B...getting it as a NodeList
Now my question is how to add this data in another XML(B.xml) at specified place.

Thanks for your patience

Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done with importNode() method but i can't see these changes in B.xml.What should i do to save the changes?

Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code... Enjoy!


package java_practice.saxpractice;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.*;

// For write operation
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;


public class TransformationApp02
// Global values
static Document documentA;
static Document documentB;

public static void main (String argv [])

//Directory path where the files are located.
String directorypath = argv[0];

String aXml = directorypath+"A.xml";
String bXml = directorypath+"B.xml";

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try {
File fA = new File(aXml);
File fB = new File(bXml);

DocumentBuilder builder = factory.newDocumentBuilder();
documentA = builder.parse(fA);
documentB = builder.parse(fB);

NodeList nodeListA = documentA.getElementsByTagName("B");
for(int count=0 ; count<nodeListA.getLength();count++)
Node nodeA = nodeListA.item(count);
Node temp = documentB.importNode(nodeA,true);
Element element = documentB.getDocumentElement();

// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();

DOMSource source = new DOMSource(documentB);
StreamResult result = new StreamResult(bXml);

transformer.transform(source, result);

} catch (TransformerConfigurationException tce) {
// Error generated by the parser
System.out.println ("\n** Transformer Factory error");
System.out.println(" " + tce.getMessage() );

// Use the contained exception, if any
Throwable x = tce;
if (tce.getException() != null)
x = tce.getException();

} catch (TransformerException te) {
// Error generated by the parser
System.out.println ("\n** Transformation error");
System.out.println(" " + te.getMessage() );

// Use the contained exception, if any
Throwable x = te;
if (te.getException() != null)
x = te.getException();

} catch (SAXException sxe) {
// Error generated by this application
// (or a parser-initialization error)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();

} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built

} catch (IOException ioe) {
// I/O error



[ March 17, 2006: Message edited by: Amy Medrat ]
Do not threaten THIS beaver! Not even with this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
    Bookmark Topic Watch Topic
  • New Topic