Calling and Testing java method - java

I have built some code to ingest an XML document and parse through the values but now I'm stuck on testing the method.
What would be the proper way to unit run test on this method? I'm also unsure how to pass an xml document to run.
public class WebServiceTools
{
static public String getVersionFromWSResponseFromDOM(Document responseDocument) {
String versionDataAsXML = badData;
try {
responseDocument.normalizeDocument();
NodeList resultList = responseDocument.getElementsByTagName("ti:VersionResponse");
Element resultElement = (Element) resultList.item(0);
if (!badData.equalsIgnoreCase(resultElement.getTextContent())) {
versionDataAsXML = resultElement.getTextContent().trim();
}
} catch (Exception e) {
e.printStackTrace();
}
return versionDataAsXML;
}
}
package org.examples.tools;
import java.lang.reflect.Method;
public class ReflectApp {
public static void main(String[] args) {
//String parameter
Class[] paramString = new Class[1];
paramString[0] = String.class;
try{
//load the AppTest at runtime
Class cls = Class.forName("org.examples.tools.WebServiceTools");
Object obj = cls.newInstance();
//call the printItString method, pass a String param
method = cls.getDeclaredMethod("printItString", paramString);
method.invoke(obj, new String(" Do I put document here? "));
}catch(Exception ex){
ex.printStackTrace();
}
}
package org.examples.tools;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
public class TestGetVersion {
public static void main (String[] args) throws Exception {
String fileName = "C:/examples/VersionResponse.xml"; // Set path to file
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(fileName));
// Create do
String result = WebServiceTools.getVersionFromWSResponseFromDOM(doc);
// Treat result
System.out.print(result);
}
}

I understand your problem is how to read an XML file into a Document, right?
There are several ways and libraries to read an XML from a file: Java: How to read and write xml files?
For instance:
String fileName = ""; // Set path to file
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(fileName));
Then, just call your method from a main function or a JUnit test:
public static void main (String[] args) {
// Create doc
String result = WebServiceTools.getVersionFromWSResponseFromDOM(doc);
// Treat result
}
UPDATE
REFLECTION
Your cls.getDeclaredMethod("printItString", paramString); is correct, although using such parameter is confusing. At first sight I thought it was a String. I'd preferably use
Method method = cls.getDeclaredMethod("printItString", new Class[] {String.class});
I think this makes it clearer (just my opinion).
To call through reflection is just what you did. Didn't it work?
Object result = method.invoke(obj, new Object[] {"whatever string"});
I assume printString is a method on WebServiceTools class, whose signature is printString(String param)
Unmarshall
First things first: As far as I know, unmarshall is usually used to convert an XML back into an Object (in XML serialization libraries, like XStream or JABX), but I guess you meant convert a Document back to String. Am I right?
If so, one way that works:
Source source = new DOMSource(doc);
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
// create a transformer
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
// set some options on the transformer
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// transform the xml document into a string
transformer.transform(source, result);
String xml = writer.getBuffer().toString();
If this is not what you meant, please clarify.

Related

force removeAttribute() from org.w3c.dom.Element

I am trying to remove an attributs from an org.w3c.dom.Element.
When using removeAttribute the attribut-name (with an default value) is still contained in the resulting XML (in this case a SVG file).
In the JavaDoc I found the following explanation:
If a default value for the removed attribute is defined in the DTD, a new attribute immediately appears with the default value as well as the corresponding namespace URI, local name, and prefix when applicable.
How can I avoid this behaviour? I can not change the DTD. I just want to get rid of this attribut.
Edit:
T.J. Crowder asked me for a minimal example.
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.*;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class MinimalExample {
public static void main(String[] args) throws Exception {
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
Document doc = f.createDocument("batik.svg");
Element svg = doc.getDocumentElement();
svg.removeAttribute("contentStyleType");
System.out.println(getXML(doc));
}
public static String getXML(Document doc) throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFact.newDocumentBuilder();
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
//transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(domSource, result);
System.out.println("XML IN String format is: \n" + writer.toString());
return writer.toString();
}
}

problem with xml document in java

i am writnig a simple program that get a string and translate that to a xml document but it dont show value of content i set it show null!
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Server {
/**
* #param args
*/
public static void main(String[] args) {
Server server=new Server();
Document dc=server.stringToDocument("f0");
System.out.println(dc.getTextContent());
}
public org.w3c.dom.Document stringToDocument(String order)
{
org.w3c.dom.Document result=null;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db=dbf.newDocumentBuilder();
result=db.newDocument();
Element el=result.createElement("ORDER");
el.setTextContent(order);
}
catch (Exception e) {
System.out.println("DB in line 1418 exception");
}
return result;
}
}
You need to add the element to the document in order for it to be associated with the document. Try adding the line:
result.appendChild(el);
See the documentation for more information.
You are calling getTextContent on a document node. This always results in a null value as you can read in the api docs:
http://download.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Node.html#getTextContent()
You will get the value if you call dc.getFirstChild().getTextContent() (after appending the element to the document), because now you are calling getTextContent on an element node.
You need to add the line:
result.appendChild(el);
to the stringToDocument method.
To get the xml string to print you can use the following method:
public String documentToString(Document doc) {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
return xmlString;
} catch (Exception e) {
return null;
}
}

Create XML file using java

How to create a xml file and save it in some place in my machine using java..there are attributes also include in the xml file? I have found org.w3c.dom.Document but having problems with creating attributes for elements and save the xml file.
Thank You.
You can use a DOM XML parser to create an XML file using Java. A good example can be found on this site:
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("company");
doc.appendChild(rootElement);
//staff elements
Element staff = doc.createElement("Staff");
rootElement.appendChild(staff);
//set attribute to staff element
Attr attr = doc.createAttribute("id");
attr.setValue("1");
staff.setAttributeNode(attr);
//shorten way
//staff.setAttribute("id", "1");
//firstname elements
Element firstname = doc.createElement("firstname");
firstname.appendChild(doc.createTextNode("yong"));
staff.appendChild(firstname);
//lastname elements
Element lastname = doc.createElement("lastname");
lastname.appendChild(doc.createTextNode("mook kim"));
staff.appendChild(lastname);
//nickname elements
Element nickname = doc.createElement("nickname");
nickname.appendChild(doc.createTextNode("mkyong"));
staff.appendChild(nickname);
//salary elements
Element salary = doc.createElement("salary");
salary.appendChild(doc.createTextNode("100000"));
staff.appendChild(salary);
//write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:\\testing.xml"));
transformer.transform(source, result);
System.out.println("Done");
}catch(ParserConfigurationException pce){
pce.printStackTrace();
}catch(TransformerException tfe){
tfe.printStackTrace();
}
You can use Xembly, a small open source library that makes this XML creating process much more intuitive:
String xml = new Xembler(
new Directives()
.add("root")
.add("order")
.attr("id", "553")
.set("$140.00")
).xml();
Xembly is a wrapper around native Java DOM, and is a very lightweight library (I'm the author).
Have look at dom4j or jdom. Both libraries allow creating a Document and allow printing the document as xml. Both are widly used, pretty easy to use and you'll find a lot of examples and snippets.
dom4j - Quick start guide
Just happened to work at this also, use https://www.tutorialspoint.com/java_xml/java_dom_create_document.htm the example from here, and read the explanations. Also I provide you my own example:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.newDocument();
// root element
Element rootElement = doc.createElement("words");
doc.appendChild(rootElement);
while (ptbt.hasNext()) {
CoreLabel label = ptbt.next();
System.out.println(label);
m = r1.matcher(label.toString());
//System.out.println(m.find());
if (m.find() == true) {
Element w = doc.createElement("word");
w.appendChild(doc.createTextNode(label.toString()));
rootElement.appendChild(w);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:\\Users\\workspace\\Tokenizer\\tokens.xml"));
transformer.transform(source, result);
// Output to console for testing
StreamResult consoleResult = new StreamResult(System.out);
transformer.transform(source, consoleResult);
This is in the context of using the tokenizer from Stanford for Natural Language Processing, just a part of it to make an idea on how to add elements.
The output is: Billbuyedapples (I've read the sentence from a file)
I am providing an answer from my own blog. Hope this will help.
What will be output?
Following XML file named users.xml will be created.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<users>
<user uid="1">
<firstname>Interview</firstname>
<lastname>Bubble</lastname>
<email>admin#interviewBubble.com</email>
</user>
</users>
PROCEDURE
Basic steps, in order to create an XML File with a DOM Parser, are:
Create a DocumentBuilder instance.
Create a Document from the above DocumentBuilder.
Create the elements you want using the Element class and its appendChild method.
Create a new Transformer instance and a new DOMSource instance.
Create a new StreamResult to the output stream you want to use.
Use transform method to write the DOM object to the output stream.
SOURCE CODE:
package com.example.TestApp;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
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 CreateXMLFileJava {
public static void main(String[] args) throws ParserConfigurationException,
IOException,
TransformerException
{
// 1.Create a DocumentBuilder instance
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dbuilder = dbFactory.newDocumentBuilder();
// 2. Create a Document from the above DocumentBuilder.
Document document = dbuilder.newDocument();
// 3. Create the elements you want using the Element class and its appendChild method.
// root element
Element users = document.createElement("users");
document.appendChild(users);
// child element
Element user = document.createElement("user");
users.appendChild(user);
// Attribute of child element
user.setAttribute("uid", "1");
// firstname Element
Element firstName = document.createElement("firstName");
firstName.appendChild(document.createTextNode("Interview"));
user.appendChild(firstName);
// lastName element
Element lastName = document.createElement("lastName");
lastName.appendChild(document.createTextNode("Bubble"));
user.appendChild(lastName);
// email element
Element email = document.createElement("email");
email.appendChild(document.createTextNode("admin#interviewBubble.com"));
user.appendChild(email);
// write content into xml file
// 4. Create a new Transformer instance and a new DOMSource instance.
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
// 5. Create a new StreamResult to the output stream you want to use.
StreamResult result = new StreamResult(new File("/Users/admin/Desktop/users.xml"));
// StreamResult result = new StreamResult(System.out); // to print on console
// 6. Use transform method to write the DOM object to the output stream.
transformer.transform(source, result);
System.out.println("File created successfully");
}
}
OUTPUT:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
<user uid="1">
<firstName>Interview</firstName>
<lastName>Bubble</lastName>
<email>admin#interviewBubble.com</email>
</user>
</users>
You might want to give XStream a shot, it is not complicated. It basically does the heavy lifting.
I liked the Xembly syntax, but it is not a statically typed API. You can get this with XMLBeam:
// Declare a projection
public interface Projection {
#XBWrite("/root/order/#id")
Projection setID(int id);
#XBWrite("/root/order")
Projection setValue(String value);
}
public static void main(String[] args) {
// create a projector
XBProjector projector = new XBProjector();
// use it to create a projection instance
Projection projection = projector.projectEmptyDocument(Projection.class);
// You get a fluent API, with java types in parameters
projection.setID(553).setValue("$140.00");
// Use the projector again to do IO stuff or create an XML-string
projector.toXMLString(projection);
}
My experience is that this works great even when the XML gets more complicated. You can just decouple the XML structure from your java code structure.
package com.server;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import org.w3c.dom.*;
import com.gwtext.client.data.XmlReader;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class XmlServlet extends HttpServlet
{
NodeList list;
Connection con=null;
Statement st=null;
ResultSet rs = null;
String xmlString ;
BufferedWriter bw;
String displayTo;
String displayFrom;
String addressto;
String addressFrom;
Date send;
String Subject;
String body;
String category;
Document doc1;
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
System.out.print("on server");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
System.out.print("on server");
try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
//creating a new instance of a DOM to build a DOM tree.
doc1 = docBuilder.newDocument();
new XmlServlet().createXmlTree(doc1);
System.out.print("on server");
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void createXmlTree(Document doc) throws Exception {
//This method creates an element node
System.out.println("ruchipaliwal111");
try
{
System.out.println("ruchi111");
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3308/plz","root","root1");
st = con.createStatement();
rs = st.executeQuery("select * from data");
Element root = doc.createElement("message");
doc.appendChild(root);
while(rs.next())
{
displayTo=rs.getString(1).toString();
System.out.println(displayTo+"getdataname");
displayFrom=rs.getString(2).toString();
System.out.println(displayFrom +"getdataname");
addressto=rs.getString(3).toString();
System.out.println(addressto +"getdataname");
addressFrom=rs.getString(4).toString();
System.out.println(addressFrom +"getdataname");
send=rs.getDate(5);
System.out.println(send +"getdataname");
Subject=rs.getString(6).toString();
System.out.println(Subject +"getdataname");
body=rs.getString(7).toString();
System.out.println(body+"getdataname");
category=rs.getString(8).toString();
System.out.println(category +"getdataname");
//adding a node after the last child node of ssthe specified node.
Element element1 = doc.createElement("Header");
root.appendChild(element1);
Element child1 = doc.createElement("To");
element1.appendChild(child1);
child1.setAttribute("displayNameTo",displayTo);
child1.setAttribute("addressTo",addressto);
Element child2 = doc.createElement("From");
element1.appendChild(child2);
child2.setAttribute("displayNameFrom",displayFrom);
child2.setAttribute("addressFrom",addressFrom);
Element child3 = doc.createElement("Send");
element1.appendChild(child3);
Text text2 = doc.createTextNode(send.toString());
child3.appendChild(text2);
Element child4 = doc.createElement("Subject");
element1.appendChild(child4);
Text text3 = doc.createTextNode(Subject);
child4.appendChild(text3);
Element child5 = doc.createElement("category");
element1.appendChild(child5);
Text text44 = doc.createTextNode(category);
child5.appendChild(text44);
Element element2 = doc.createElement("Body");
root.appendChild(element2);
Text text1 = doc.createTextNode(body);
element2.appendChild(text1);
/*
Element child1 = doc.createElement("name");
root.appendChild(child1);
Text text = doc.createTextNode(getdataname);
child1.appendChild(text);
Element element = doc.createElement("address");
root.appendChild(element);
Text text1 = doc.createTextNode( getdataaddress);
element.appendChild(text1);
*/
}
//TransformerFactory instance is used to create Transformer objects.
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD,"xml");
// transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
// create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
xmlString = sw.toString();
File file = new File("./war/ds/newxml.xml");
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(xmlString);
}
catch(Exception e)
{
System.out.print("after while loop exception"+e.toString());
}
bw.flush();
bw.close();
System.out.println("successfully done.....");
}
}

How can I append an attribute to an existing XML element in Java?

I want to append an attribute an existing element in XML using Java. For example:
<employee>
<details name="Jai" age="25"/>
<details name="kishore" age="30"/>
</employee>
It want to add weight to it (assume that it is calculated and then appended in response). How can I append that to all items?
<details name="Jai" age="25" weight="55"/>
import org.w3c.dom.*;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class AddAndPrint {
public static void main(String[] args) {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse("/path/to/file.xml");
NodeList employees = document.getElementsByTagName("employee");
for (Node employee : employees) {
for (Node child : employee.getChildNodes() {
if ("details".equals(child.getNodeName()) child.setAttribute("weight", "150");
}
}
try {
Source source = new DOMSource(doc);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
System.out.println(stringWriter.getBuffer().toString());
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
Here is a quick solution based on jdom:
public static void main(String[] args) throws JDOMException, IOException {
File xmlFile = new File("employee.xml");
SAXBuilder builder = new SAXBuilder();
Document build = builder.build(xmlFile);
XPath details = XPath.newInstance("//details");
List<Element> detailsNodes = details.selectNodes(build);
for (Element detailsNode:detailsNodes) {
detailsNode.setAttribute("weight", "70"); // static weight for demonstration
}
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(build, System.out);
}
First, we build a document (SAXBuilder), next we create a XPath expression for the details node, then we iterate through the elements for that expression and add the weight attribute.
The last two lines just verify that it's white magic :-)

How to I output org.w3c.dom.Element to string format in java?

I have an org.w3c.dom.Element object passed into my method. I need to see the whole xml string including its child nodes (the whole object graph). I am looking for a method that can convert the Element into an xml format string that I can System.out.println on. Just println() on the 'Element' object won't work because toString() won't output the xml format and won't go through its child node. Is there an easy way without writing my own method to do that? Thanks.
Assuming you want to stick with the standard API...
You could use a DOMImplementationLS:
Document document = node.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document
.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
String str = serializer.writeToString(node);
If the <?xml version="1.0" encoding="UTF-16"?> declaration bothers you, you could use a transformer instead:
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node),
new StreamResult(buffer));
String str = buffer.toString();
Simple 4 lines code to get String without xml-declaration (<?xml version="1.0" encoding="UTF-16"?>) from org.w3c.dom.Element
DOMImplementationLS lsImpl = (DOMImplementationLS)node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer serializer = lsImpl.createLSSerializer();
serializer.getDomConfig().setParameter("xml-declaration", false); //by default its true, so set it to false to get String without xml-declaration
String str = serializer.writeToString(node);
Try jcabi-xml with one liner:
String xml = new XMLDocument(element).toString();
Not supported in the standard JAXP API, I used the JDom library for this purpose. It has a printer function, formatter options etc. http://www.jdom.org/
If you have the schema of the XML or can otherwise create JAXB bindings for it, you could use the JAXB Marshaller to write to System.out:
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
#XmlRootElement
public class BoundClass {
#XmlAttribute
private String test;
#XmlElement
private int x;
public BoundClass() {}
public BoundClass(String test) {
this.test = test;
}
public static void main(String[] args) throws Exception {
JAXBContext jxbc = JAXBContext.newInstance(BoundClass.class);
Marshaller marshaller = jxbc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.marshal(new JAXBElement(new QName("root"),BoundClass.class,new Main("test")),System.out);
}
}
this is what i s done in jcabi:
private String asString(Node node) {
StringWriter writer = new StringWriter();
try {
Transformer trans = TransformerFactory.newInstance().newTransformer();
// #checkstyle MultipleStringLiterals (1 line)
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.VERSION, "1.0");
if (!(node instanceof Document)) {
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}
trans.transform(new DOMSource(node), new StreamResult(writer));
} catch (final TransformerConfigurationException ex) {
throw new IllegalStateException(ex);
} catch (final TransformerException ex) {
throw new IllegalArgumentException(ex);
}
return writer.toString();
}
and it works for me!
With VTD-XML, you can pass into the cursor and make a single getElementFragment call to retrieve the segment (as denoted by its offset and length)... Below is an example
import com.ximpleware.*;
public class concatTest{
public static void main(String s1[]) throws Exception {
VTDGen vg= new VTDGen();
String s = "<users><user><firstName>some </firstName><lastName> one</lastName></user></users>";
vg.setDoc(s.getBytes());
vg.parse(false);
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/users/user/firstName");
int i=ap.evalXPath();
if (i!=1){
long l= vn.getElementFragment();
System.out.println(" the segment is "+ vn.toString((int)l,(int)(l>>32)));
}
}
}

Categories

Resources