Parsing using Dom produces type mismatch - java

Given the following code , under Eclipse , I get a type mismatch error :
package xmlInterface;
import javax.swing.text.*;
import org.w3c.dom.*;
import org.w3c.dom.Document;
import gameManage.round;
import java.io.File;
import javax.lang.model.element.Element;
import javax.swing.text.Segment;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import com.sun.org.apache.bcel.internal.classfile.Method;
public void writeToXml(round[] games) throws ParserConfigurationException
{
int i;
// build a doucument by the parser
DocumentBuilderFactory document = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = document.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("GameOut");
...
...
...
}
I get the following error in Eclipse :
Type mismatch: cannot convert from org.w3c.dom.Element to javax.lang.model.element.Element
Can anyone please explain how can I fix this ?
Thank you
Jason

I think you've mistaken an import. Not
import javax.lang.model.element.Element;
but
import org.w3c.dom.Element;
Don't use import with * like
org.w3c.dom.*
otherwise you'd be likely to get some "hiding" error, since the last "Element" import you've coded (javax.lang.model.element.Element) will hide the org.w3c.dom.Element included in the import org.w3c.dom.* line.

Related

How to disable newline and indenting in DOM Transformer?

I am parsing an XML document and serializing it back. I want to preserve the original indentation and newlines as much as possible. I do not wish to pretty print the DOM.
Consider the code below where the original document has a single new line before and after the <div> tag and no indentation.
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import java.io.StringReader;
public class Test {
public static void main(String args[]) throws Exception {
String xmlIn = "<html>\n<div>Hello World!</div>\n</html>";
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = documentBuilder.parse(new InputSource(new StringReader(xmlIn)));
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer domTransformer = transfac.newTransformer();
domTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
domTransformer.setOutputProperty(OutputKeys.INDENT, "no");
StreamResult result = new StreamResult(System.out);
DOMSource source = new DOMSource(doc);
domTransformer.transform(source, result);
}
}
The expectation is to see this:
<html>
<div>Hello World!</div>
</html>
But, Java 8 is printing:
<html>
<div>Hello World!</div>
</html>
Java 11 is printing:
<html>
<div>Hello World!</div>
</html>
As you can see, they are both adding an extra new line. Java 11 has also added indentation.
How can I prevent adding extra new lines and indentations?

Compilation error "type mismatch" at line Cookie cookie[]= req.getCookies();

I am currently using the latest version of eclipse and tomcat server(v8.5)
type mismatch error is being generated in SqServlets.java file in "Cookie cookie[]= req.getCookies();" line of my code.
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.tomcat.util.http.parser.Cookie;
public class SqServlets extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException {
Cookie cookie[]= req.getCookies();
// ...
}
}
I think that the error is in the import:
import org.apache.tomcat.util.http.parser.Cookie;
From the documentation of HttpServletRequest we have that the method getCookies returns a javax.servlet.http.Cookie that is not compatible with your declaration.
So fix the import with:
import javax.servlet.http.Cookie;
And then use the relative API to find and get the value of your cookie.

WritableWorkbook cannot be resolved to a type even after importing necessary executable JARs

I am trying to create a new excel file using below code. I have added all the necessary JAR files as you can see from import code lines. But I am still seeing the error that "WritableWorkbook cannot be resolved to a type" and "Workbook can not be resolved to a type".
package myPackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.WebElement;
import java.util.List;
import java.io.File;
import java.io.OutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
public class myClass {
public static void main(String [] args)
{
File eFile = new File ("C:\\Users\\bondrea\\AutomationRelatedFiles\\TrialExcel.xlsx");
WritableWorkbook FinalExcel = Workbook.createWorkbook(eFile);
}
}

File not found exception when using XPath for XML parsing

I am trying to write a Java program to add attribute to an already existing XML file.The XML is :
<Items ApplyDifferences="Y" ValidateItems="Y" CompleteInventoryFlag="Y" ShipNode=" ACMEUSDC" >
<Item InventoryOrganizationCode="ACMEUS" ItemID="2000033672234"
ProductClass="GOOD" UnitOfMeasure="EACH">
<Supplies>
<Supply AvailabilityType="TRACK" Quantity="20.00" ShipByDate="2500-01-01"
SupplyType="ONHAND"/>
</Supplies>
</Item>
<Item InventoryOrganizationCode="ACMEUS" ItemID="2000033672235"
ProductClass="GOOD" UnitOfMeasure="EACH">
<Supplies>
<Supply AvailabilityType="TRACK" Quantity="25.00" ShipByDate="2500-01-01"
SupplyType="ONHAND"/>
</Supplies>
</Item>
<Item InventoryOrganizationCode="ACMEUS" ItemID="2000033672236"
ProductClass="GOOD" UnitOfMeasure="EACH">
<Supplies>
<Supply AvailabilityType="TRACK" Quantity="25.00" ShipByDate="2500-01-01"
SupplyType="ONHAND"/>
</Supplies>
</Item>
</Items>
My Java code is:
package xmltest;
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 javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Testxml4 {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException {
// TODO Auto-generated method stub
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File inputFile = new File("C:/Users/praveen.sharma/Desktop/XMLs/xml4.xml");
System.out.println(new File(".").getAbsolutePath());
System.out.println(inputFile.exists());
Document doc = builder.parse(inputFile);
Element element = (Element) XPathAPI.selectSingleNode(doc,"Order/OrderLines/OrderLine[#PrimeLineNo='6']/OrderLineSourcingControls/OrderLineSourcingCntrl[#Node='Node1']");
element.setAttribute("Node", "Node02");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(inputFile);
transformer.transform(source, result);
}
}
This is the error I am getting:
Exception in thread "main" javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:\C:\Users\praveen.sharma\Desktop\XMLs\xml4.xml (The filename, directory name, or volume label syntax is incorrect)
at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:263)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:296)
at xmltest.Testxml4.main(Testxml4.java:46)
Caused by: java.io.FileNotFoundException: file:\C:\Users\praveen.sharma\Desktop\XMLs\xml4.xml (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:253)
... 2 more
---------
java.io.FileNotFoundException: file:\C:\Users\praveen.sharma\Desktop\XMLs\xml4.xml (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:253)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:296)
at xmltest.Testxml4.main(Testxml4.java:46)
I can assure you my XML is present at the specified path and has all required permission.
You are trying to write the output to the same file from where you are reading. You should write to a different file instead.
If you want to "change" the same file, then the safest way is to write to a temporary file, then close all streams, then delete the original file and then rename the temporary file to the original name.

Fatal Error - Content is not allowed in prolog

Similar to this question, but unfortunately didn't help
I am trying to parse a String to XML in Java and keep getting the error:
[Fatal Error] output.txt:1:1: Content is not allowed in prolog.
I know it must be something to do with my XML string, because I ran a test with very basic XML and the error dissappeared.
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="ff99v250_01" APIVersion="1.4.3139.0"?>
<jfxpf:XPF xmlns:jfxpf="http://www.xfa.com/schema/xml-package">
<jfxpf:Package>
<jfxpf:Resource Location="GenReq">
<jfxpf:Link ContentType="application/x-jetform-cft" />
</jfxpf:Resource>
<jfxpf:Resource Location="default.xml">
<jfxpf:Content ContentType="text/xml" Location="default.xml">
<xfa:Data xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:DataGroup>
<data xmlns:xfe="http://www.xfa.org/schema/xfa-events/1.0" xfe:script="$config.proto.common.template.uri='GenReq'" xfe:event="$config:load">
<?jetform ^Dat ^page 1?>
<FR_NAME>Administrator</FR_NAME>
<JFWF_DELEGATE />
<ADHOC_DLN_ACTOR />
<ADHOC_DLN_MSG />
<ADHOC_DLN_TIME />
<ADHOC_DLN_UNITS>Days</ADHOC_DLN_UNITS>
<ADHOC_RMD_MSG />
<ADHOC_RMD_TIME />
<ADHOC_RMD_UNITS>Days</ADHOC_RMD_UNITS>
<ADHOC_RPT_TIME />
<ADHOC_RPT_UNITS>Days</ADHOC_RPT_UNITS>
<CIRCULATETO />
<COMPLETION />
<FOLLOWUP />
<MSGSUBJECT />
<OTHERFIELD />
<PRIORITY>Low</PRIORITY>
<REQUEST />
<RESPONSE />
<Submit />
<ADHOC_VALIDDATA>True</ADHOC_VALIDDATA>
<JFWF_TRANID>2xxyg9sffane7pwd5j8yv9t49s.1</JFWF_TRANID>
<JFWF_INSTRUCTION>Initiate a General Request. Fill the request form, then identify the next participant.</JFWF_INSTRUCTION>
<JFWF_TRANSPORT>HTTP</JFWF_TRANSPORT>
<JFWF_STATUS>RECEIVED</JFWF_STATUS>
<JFWF_ACTION />
<JFWF_CHOICE>*Select Next Participant,Cancel</JFWF_CHOICE>
<JFWF_VERSION>6.2</JFWF_VERSION>
<JFWF_READONLY>1</JFWF_READONLY>
</data>
</xfa:DataGroup>
</xfa:Data>
</jfxpf:Content>
</jfxpf:Resource>
</jfxpf:Package>
</jfxpf:XPF>
However, I am having trouble finding the text that is causing this issue. My Java code is below:
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new InputSource(new StringReader(xml)));
EDIT
Removing the Data node works, so the error is somewhere deep in the XML. This does not throw an error:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<?xfa generator=\"ff99v250_01\" APIVersion=\"1.4.3139.0\"?>
<jfxpf:XPF xmlns:jfxpf=\"http://www.xfa.com/schema/xml-package\">
<jfxpf:Package>
<jfxpf:Resource Location=\"GenReq\">
<jfxpf:Link ContentType=\"application/x-jetform-cft\"/>
</jfxpf:Resource>
<jfxpf:Resource Location=\"default.xml\">
<jfxpf:Content ContentType=\"text/xml\" Location=\"default.xml\">
</jfxpf:Content>
</jfxpf:Resource>
</jfxpf:Package>
</jfxpf:XPF>
My Imports
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JFileChooser;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
My guess is that the file starts with a BOM character U+FEFF: error at line 1, column 1.This is a zero-width space used sometimes to mark a file as being in some Unicode representation, UTF-8, UTF-16LE, UTF-16BE.
The BOM character can be removed. Check the file size, and then look what options you have: save as UTF-8 without BOM, delete.
In java (should the editor be stubborn):
Path path = Paths.get(".... .xml");
byte[] content = Files.readAllBytes(path);
String s = new String(content, StandardCharsets.UTF_8);
s = s.replaceFirst("^\uFEFF", "");
byte[] content2 = s.getBytes(StandardCharsets.UTF_8);
if (content2.length != content.length) {
Files.write(path, content2);
}
The document and sample code you provided works fine in Java 1.8u25:
import static org.junit.Assert.*;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class FatalErrorTest
{
#Test
public void as_given() throws SAXException, IOException, ParserConfigurationException
{
String xml ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<?xfa generator=\"ff99v250_01\" APIVersion=\"1.4.3139.0\"?>\r\n<jfxpf:XPF xmlns:jfxpf=\"http://www.xfa.com/schema/xml-package\">\r\n <jfxpf:Package>\r\n <jfxpf:Resource Location=\"GenReq\">\r\n <jfxpf:Link ContentType=\"application/x-jetform-cft\" />\r\n </jfxpf:Resource>\r\n <jfxpf:Resource Location=\"default.xml\">\r\n <jfxpf:Content ContentType=\"text/xml\" Location=\"default.xml\">\r\n <xfa:Data xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\">\r\n <xfa:DataGroup>\r\n <data xmlns:xfe=\"http://www.xfa.org/schema/xfa-events/1.0\" xfe:script=\"$config.proto.common.template.uri='GenReq'\" xfe:event=\"$config:load\">\r\n <?jetform ^Dat ^page 1?>\r\n <FR_NAME>Administrator</FR_NAME>\r\n <JFWF_DELEGATE />\r\n <ADHOC_DLN_ACTOR />\r\n <ADHOC_DLN_MSG />\r\n <ADHOC_DLN_TIME />\r\n <ADHOC_DLN_UNITS>Days</ADHOC_DLN_UNITS>\r\n <ADHOC_RMD_MSG />\r\n <ADHOC_RMD_TIME />\r\n <ADHOC_RMD_UNITS>Days</ADHOC_RMD_UNITS>\r\n <ADHOC_RPT_TIME />\r\n <ADHOC_RPT_UNITS>Days</ADHOC_RPT_UNITS>\r\n <CIRCULATETO />\r\n <COMPLETION />\r\n <FOLLOWUP />\r\n <MSGSUBJECT />\r\n <OTHERFIELD />\r\n <PRIORITY>Low</PRIORITY>\r\n <REQUEST />\r\n <RESPONSE />\r\n <Submit />\r\n <ADHOC_VALIDDATA>True</ADHOC_VALIDDATA>\r\n <JFWF_TRANID>2xxyg9sffane7pwd5j8yv9t49s.1</JFWF_TRANID>\r\n <JFWF_INSTRUCTION>Initiate a General Request. Fill the request form, then identify the next participant.</JFWF_INSTRUCTION>\r\n <JFWF_TRANSPORT>HTTP</JFWF_TRANSPORT>\r\n <JFWF_STATUS>RECEIVED</JFWF_STATUS>\r\n <JFWF_ACTION />\r\n <JFWF_CHOICE>*Select Next Participant,Cancel</JFWF_CHOICE>\r\n <JFWF_VERSION>6.2</JFWF_VERSION>\r\n <JFWF_READONLY>1</JFWF_READONLY>\r\n </data>\r\n </xfa:DataGroup>\r\n </xfa:Data>\r\n </jfxpf:Content>\r\n </jfxpf:Resource>\r\n </jfxpf:Package>\r\n</jfxpf:XPF>";
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new InputSource(new StringReader(xml)));
assertNotNull(doc);
}
}

Categories