Here I try to access two third party API.
I got two xml response, I merge them in one single file and store it in local system.
If i print the output in console I got the output in xml format, but I want to print it in the browser.
My solution won't work please help me.
Here's my code:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml");
PrintWriter out=response.getWriter();
String btn1=request.getParameter("btn1");
String btn2=request.getParameter("btn2");
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setIgnoringComments(true);
DocumentBuilder builder = null;
try {
builder = domFactory.newDocumentBuilder();
} catch (ParserConfigurationException e2) {
e2.printStackTrace();
}
Document doc = null;
try {
doc = builder.parse(new URL("valid url in my program").openStream());
response.setContentType("text/xml");
Object con=doc.getDoctype();
} catch (SAXException e2) {
e2.printStackTrace();
}
Document doc1 = null;
try {
doc1 = builder.parse(new URL(valid url).openStream());
} catch (SAXException e2) {
e2.printStackTrace();
}
NodeList nodes = doc.getElementsByTagName("events");
NodeList node1=doc.getElementsByTagName("events");
Element root=doc.getDocumentElement();
Element root1 = doc.createElement("ObjectId");
doc.getDocumentElement().appendChild(root1);
root.getElementsByTagName("ObjectId").item(0).setTextContent("1");
node1.item(0).getParentNode().insertBefore(root1,node1.item(0));
NodeList nodes1 = doc1.getElementsByTagName("events");
NodeList node2=doc1.getElementsByTagName("event");
Element root2=doc1.getDocumentElement();
Element root3= doc1.createElement("ObjectId");
doc1.getDocumentElement().appendChild(root3);
root2.getElementsByTagName("ObjectId").item(0).setTextContent("2");
node2.item(0).getParentNode().insertBefore(root3,node2.item(0));
for(int i=0;i<nodes1.getLength();i=i+1){
Node n= (Node) doc.importNode(nodes1.item(i), true);
nodes.item(i).getParentNode().appendChild(n);
}
Transformer transformer = null;
try {
transformer = TransformerFactory.newInstance().newTransformer();
} catch (TransformerConfigurationException e1) {
e1.printStackTrace();
} catch (TransformerFactoryConfigurationError e1) {
e1.printStackTrace();
}
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
try {
transformer.transform(source,result);
} catch (TransformerException e) {
e.printStackTrace();
}
Writer output = null;
output = new BufferedWriter(new FileWriter("merge.xml"));
String xmlout = result.getWriter().toString();
output.write(xmlout);
response.setContentType("text/xml");
out.write(xmlout);
//out.println(xmlout);
//System.out.println(xmlout);
//I tried many ways but
//it will not print to the browser in xml the format
}
A point of note; you're setting the ContentType three times. It only needs setting once?
You don't appear to be returning the response, but I presume some other part of your program is handling that.
EDITED: the other answer that was here disappeared.
Going to need more information to work out what is going wrong. Please bear in mind that System.out.println() calls will not show up in the browser as they're console-specific.
Related
Is there any way to prevent Java from escaping the special characters when writing to XML using DOM? I can’t change the format of the XML so I can’t save them in CDATA tags.
I’m trying to save HTML inside a XML file using DOM. Currently it is escaping the special characters so <p> is being saved as <p>.
public void updateXML(Document doc) throws XPathExpressionException{
Node aNode = getXMLNode("//PRECISSCHE.HTM/html/body", doc);
removeChilds(aNode);
aNode.setTextContent(saved_precis_Scheme);
}
It's outputting as:
<PRECISSCHE.HTM>
<html>
<body><p>schemeName</p></body>
</html>
</PRECISSCHE>
but I need:
<PRECISSCHE.HTM>
<html>
<body><p>schemeName</p></body>
</html>
</PRECISSCHE>
Here is my code to create the doc object:
//pull data and write to XML
public void updateXML(String path) throws XPathExpressionException{
try {
String filepath = path;
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
Tab_Client.updateClientXML(doc);
if(!polType.equals("Household")){
Tab_Vehicle.updateVehicleXML(doc);
Tab_PCClaimsConv.updateXML(doc);
Tab_precis.updateXML(doc);
}
if(polType.equals("Household")){
Tab_PropertyDetails.updatePropertyDetailsXML(doc);
Tab_HCsumInsured.updateXML(doc);
Tab_Contents.updateXML(doc);
Tab_HCClaims.updateXML(doc);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
You can that do
stringString.replaceAll("<","<").replaceAll(">",">")
before you output the data
I've been reading over this SAXParseException error for a while and trying to resolve it but to no success.
[Fatal Error] schedule.xml:1:1: Premature end of file.
org.xml.sax.SAXParseException; systemId: file:/U:/schedule.xml; lineNumber: 1; columnNumber: 1; Premature end of file.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at package.DOMclass.importDocument(DOMclass.java:681) //<---.parse(new File(fileToImport));
...
I've read how it could be the stream being used is empty (or xml file being read is empty), as well as it can't be reused? Also, the problem of reading and writing back to the same file. Here's a link that explains it but I still don't understand.
Also, another possibility could be that my xml is malformed/not correct but I've looked over it and it's fine, the tags are good and I've encoded it in UTF-8 (without BOM) in Notepad++ since some posts have said there might be hidden whitespace before the xml declaration but again, no success.
This is my reading in code
public TestClass(){
Node root = DOMclass.importDocument("U:\\schedule.xml");
... read nodes, attribute values, etc..
}
public static Document importDocument(String fileToImport)
{
Document document = null;
try
{
document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new File(fileToImport)); //<--- where the error is at
//Do I need a Reader here? to close?
}
catch(SAXException saxe)
{
saxe.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(ParserConfigurationException pce)
{
pce.printStackTrace();
}
return document;
}
This is my writing out.
public static void addNode(String name, String lastName,...){
String filepath = "U;\\schedule.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
try {
docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
...
... make/traverse xml nodes, elements, appending
...
}
DOMUtils.exportDocument(doc, filepath);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void exportDocument(Document documentToExport, String fileName)
{
Transformer transformer = null;
DOMSource domSource = null;
StreamResult streamResult = null;
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
transformer = TransformerFactory.newInstance().newTransformer();
domSource = new DOMSource(documentToExport);
streamResult = new StreamResult(out);
System.out.println("\nStream Result: " + streamResult);
transformer.transform(domSource, streamResult);
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch(TransformerException e)
{
e.printStackTrace();
}
finally
{
transformer = null;
domSource = null;
streamResult = null;
}
}
}
Any thoughts? Thank you.
I have been recently trying to read from a xml file. I believe I am parsing correctly but it returns null,however not always.I received the input that I want 1 out of 100 executions, with no drastic changes on code.This is the code that receives the file path of xml file(also file in code)
public Document getXmlDoc(String filePath) {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
Document doc = null;
try {
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(new File(filePath));
doc.getDocumentElement().normalize();
System.out.println(doc.getDocumentElement().getNodeValue());//null
System.out.println(doc.getDocumentElement().getChildNodes().item(1));//null
System.out.println(doc.getDocumentElement().getLastChild().getAttributes().getNamedItem("id"));//not null and correct
System.out.println(doc.getDocumentElement().getElementsByTagName("entry").item(1));//null
} catch (ParserConfigurationException e) {
e.printStackTrace();//pipe these
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return doc;
}
Also I call my method here and turn my node to string
NodeList nList = getXmlDoc(newFilePath).getElementsByTagName("entry");
System.out.println(nodeToString(nList.item(i)));
Where nList is a node list and nodeToString method is like this:
private String nodeToString(Node node){
StringWriter sw = new StringWriter();
try{
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
t.transform(new DOMSource(node),new StreamResult(sw));
} catch (TransformerException e) {
e.printStackTrace();
}
return sw.toString();
}
In my android application I am using an xml file to store some history information within the application.
Following is the code I use to enter a new record to the file.
String filename = "file.xml";
File xmlFilePath = new File("/data/data/com.testproject/files/" + filename);
private void addNewRecordToFile(History history)
{
try
{
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.parse(xmlFilePath);
Element rootEle = doc.getDocumentElement();
Element historyElement = doc.createElement("History");
rootEle.appendChild(historyElement);
Element customerEle = doc.createElement("customer");
customerEle.appendChild(doc.createTextNode(history.getCustomer()));
historyElement.appendChild(customerEle);
Element productEle = doc.createElement("product");
productEle.appendChild(doc.createTextNode(history.getProduct()));
historyElement.appendChild(productEle);
//-------->
DOMSource source = new DOMSource(doc);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StreamResult result = new StreamResult(xmlFilePath);
transformer.transform(source, result);
}
catch (ParserConfigurationException e)
{
Log.v("State", "ParserConfigurationException" + e.getMessage());
}
catch (SAXException e)
{
Log.v("State", "SAXException" + e.getMessage());
}
catch (IOException e)
{
Log.v("State", "IOException" + e.getMessage());
}
catch (TransformerConfigurationException e) {
e.printStackTrace();
}
catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
}
catch (TransformerException e) {
e.printStackTrace();
}
}
XML file format
<?xml version="1.0" encoding="UTF-8"?>
<HistoryList>
<History>
<customer>Gordon Brown Ltd</customer>
<product>Imac</product>
</History>
<History>
<customer>GG Martin and Sons</customer>
<product>Sony Vaio</product>
</History>
<History>
<customer>PR Thomas Ltd</customer>
<product>Acer Laptop</product>
</History>
</HistoryList>
So using this code I can successfully add a new rocord to the file. But My minimum target version in android shoud be API level 4. This code works well with API Level 8 and above.
DOMSource, TransformerFactory classes are not available in android API levels under 8. So All the things before the comment //--------> works in APIs below 8.
Does anyone know any way that I can write to the xml file without using Transformer APIs. Thanks in advance...
EDITS.....
In my case I have to use a xml file to store information. That's why I don't look for sharedpreferences or Sqlite DB to store data. Thanks.
Maybe you should store this information is SharedPreferences instead? If you have a specific reason why you are trying to write this to XML, that is fine.
Otherwise, I would suggest using a different method of persistence in Android - as there are a few built in ways to do this that are easier that working with XML.
I written a multiple thread program, each thread need to parse a xml file and update new value. The problem: assuming i have a xml contents of ABC, now thread A parse the xml and update it become ABCA, at the same time thread B also parse the xml (which the content is ABC) and update it become ABCB. thread B update the xml after thread A updated, so the result of the xml is ABCB, what i want is the xml result should be ABCAB. Any idea to control the way of parsing and update in the thread?
here's my code:
WSthread.java
public class WSthread extends Thread {
public String WSname;
Process proc= null;
WebServicesXML xml;
WSthread(String name){
WSname=name;
}
public void run() {
try {
//my code
// Run a java app in a separate system process
String cmd = (WSname);
proc = Runtime.getRuntime().exec("java -jar "+cmd);
xml = new WebServicesXML();
//Process proc = Runtime.getRuntime().exec("java -jar .jar");
// Then retreive the process output
//InputStream in = proc.getInputStream();
//InputStream err = proc.getErrorStream();
BufferedReader is = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
String regex = "\\bhttp\\b";
Pattern pattern = Pattern.compile(regex);
String WSaddress = "";
while ((line = is.readLine()) != null){
Matcher matcher = pattern.matcher(line);
if(matcher.find()){
WSaddress = line;
System.out.println("Updating WS address..."+WSaddress);
xml.create(WSname, WSaddress);
}
System.out.println(line);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public void close(){
proc.destroy();
}
WebServicesXML.java
public class WebServicesXML{
public int totalWebServices;
public String WSnamelist[];
public synchronized void create(String WSname, String WSaddress) throws OException, TransformerConfigurationException, TransformerException, ParserConfigurationException{
try {
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document readdoc = docBuilder.parse("webservices.xml");
// normalize text representation
readdoc.getDocumentElement ().normalize ();
Node rootList = readdoc.getDocumentElement();
Element rootElement = (Element)rootList;
Element webservice = readdoc.createElement("WebService");
Element name = readdoc.createElement("name");
Element address = readdoc.createElement("address");
name.setTextContent(WSname);
address.setTextContent(WSaddress);
webservice.appendChild(name);
webservice.appendChild(address);
rootElement.appendChild(webservice);
/////////////////
//Output the XML
//set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
//create string from xml tree
File file = new File("webservices.xml");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(file);
DOMSource source = new DOMSource(readdoc);
trans.transform(source, result);
}
catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The basic problem is a seperate DOM is built in every thread for a single transform to be applied. This means that the last thread to run 'wins' in terms of writing its content back to the XML file.
On the one hand you are using threads, I assume for performance, but on the other hand you parse and serialize the XML multiple times. And the threading implementation is unsafe.
My recommendation is to remove the threading and do the changes in loop. When it's working you can measure the performance and THEN choose to look at an implementation using threads.
Remember, premature optimization is the root of all evil.
You need to make sure that only one thread is accessing the file at any time. You can use the native synchronization tools available for that (such as the synchronized keyword and the wait()/notify() mechanism) or you can look into higher-level synchronization tools such as Semaphore or FileLock.
When you synchronize a method the lock is taken on the object (for non-static methods) so because each thread gets its own instance of WebServicesXML each can obtain a lock and no blocking occurs.
The easiest way to deal with this would be to only create a single instance of WebServicesXML and pass that single instance to each thread when it is created.
Alternatively create a semaphore object in WebServicesXML like this:
private static final Object FILE_SEMAPHORE = new Object();
And then add a synchronized block round the lines that update the file like this:
synchronized (FILE_SEMAPHORE) {
//File update code goes here.
}