jQuery in Action, 3rd edition
The moose likes XML and Related Technologies and the fly likes XPath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XPath" Watch "XPath" New topic


Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
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

Joined: Aug 27, 2004
Posts: 328
Done with importNode() method but i can't see these changes in B.xml.What should i do to save the changes?

Amy Medrat

Joined: Mar 05, 2006
Posts: 15
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;
import javax.xml.transform.stream.StreamResult;

import java.io.*;

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 ]

I agree. Here's the link: http://aspose.com/file-tools
subject: XPath
It's not a secret anymore!