Fatal Error - Content is not allowed in prolog - java

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);
}
}

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?

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.

java copy-of xsl transformation removes namespace prefix

I made a minimal example to reproduce the problem. This is the transformation (mini.xsl):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<foo>
<xsl:copy-of select="."/>
</foo>
</xsl:template>
</xsl:stylesheet>
and this is the input (mini.xml):
<?xml version="1.0" encoding="utf-8"?>
<bar xmlns:x="baz">
<x:baz/>
</bar>
When I apply the transformation with
xsltproc mini.xsl mini.xml
the result looks as expected:
<?xml version="1.0"?>
<foo>
<bar xmlns:x="baz">
<x:baz/>
</bar>
</foo>
However, when I run the transformation with the following Java program,
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class Program
{
public static void main(String[] args)
{
try
{
Source transform = new StreamSource(new FileInputStream(args[0]));
Templates templates = TransformerFactory.newInstance().newTemplates(transform);
Document input = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream(args[1]));
Result result = new StreamResult(System.out);
templates.newTransformer().transform(new DOMSource(input), result);
}
catch (TransformerFactoryConfigurationError | ParserConfigurationException | SAXException | IOException | TransformerException e)
{
e.printStackTrace();
}
}
}
the result looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<bar xmlns:x="baz">
<baz/>
</bar>
</foo>
(notice that the x: prefix in front of baz is missing.)
Why is that?
And what can I do about it (to preserve the namespace prefix)?
For what I can only assume are historical reasons, DocumentBuilderFactory is non-namespace-aware by default. You need to explicitly switch on namespaces before you do newDocumentBuilder().
It would also be better to use the parse method that takes a File directly rather than creating your own FileInputStream (which your code is not closing once the parse is finished), and likewise with the StreamSource from which the Transformer is built.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document input = dbf.newDocumentBuilder().parse(new File(args[1]));

Flying saucer ignoring css

I have trouble displaying my xml file with the attached CSS. Below is my java code
package mainPackage;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class pdf_test {
public static void main(String[] args) throws IOException, DocumentException {
String inputFile = "/Users/jgonzo4/Desktop/Coen174/COEN174/output.xml";
String outputFile = "test1.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File(inputFile));
renderer.layout();
renderer.createPDF(os);
os.close();
}
}
The pdf gets created but it is just blank. Below is the xml file
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/Users/jgonzo4/Desktop/Coen174/COEN174/src/mainPackage/test.css"?>
<program_of_studies>
<student_info firstName="Jesus" lastName="Gonzalez" primaryEmail="email" secondaryEmail="email" home_phone="number" advisor="Teacher" last_modified="1383613851235" />
<grad_core_reqs />
<appl_math />
<focus_area_core />
<other_area_core />
<addit_elect_courses />
<transfer_cred_reqs />
<meta_data version="1.0.0" />
<history />
Here is the CSS file that should work
student_info:before {
font-size: 115%;
content:attr(firstName)""attr(middleName)" "attr(lastName)" "attr(primaryEmail);
display: block;
}
student_info:after {
font-size: 115%;
content:attr(secondaryEmail)" "attr(home_phone)" "attr(advisor);
display: block;
}
This works exactly the way I want it on a browser so I'm guessing that I am doing something wrong in my java function. Please let me know of any suggestions

Parsing using Dom produces type mismatch

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.

Categories