• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XPath

 
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......?>
<a>
<B>
<c>Some data</c>
</B>
<B>
<c>Another data</c>
</B>

<d>
<x>hi to all</x>
</d>
</a>

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...........?>
<note>

</note>



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

</note>

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
Regards

Shrinivas
 
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
hey,
Done with importNode() method but i can't see these changes in B.xml.What should i do to save the changes?

Shrinivas
 
Greenhorn
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!

----------------------------START--------------------------------

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();
element.appendChild(temp);
}

// 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();
x.printStackTrace();

} 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();
x.printStackTrace();

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

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

} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}

}

}


-----------------------------END---------------------------------
[ March 17, 2006: Message edited by: Amy Medrat ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic