how to disable dtd at runtime in java's xpath?
I got dtd in file and I cant remove it. When i try to parse it in Java I get "Caused by: java.net.SocketException: Network is unreachable: connect", because its remote dtd. can I disable somehow dtd checking?
Asked by: Connie560 | Posted: 28-01-2022
Answer 1
You should be able to specify your own EntityResolver, or use specific features of your parser? See here for some approaches.
A more complete example:
<?xml version="1.0"?>
<!DOCTYPE foo PUBLIC "//FOO//" "foo.dtd">
<foo>
<bar>Value</bar>
</foo>
And xpath usage:
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Main {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
System.out.println("Ignoring " + publicId + ", " + systemId);
return new InputSource(new StringReader(""));
}
});
Document document = builder.parse(new File("src/foo.xml"));
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String content = xpath.evaluate("/foo/bar/text()", document
.getDocumentElement());
System.out.println(content);
}
}
Hope this helps...
Answered by: Freddie129 | Posted: 01-03-2022Answer 2
This worked for me:
SAXParserFactory saxfac = SAXParserFactory.newInstance();
saxfac.setValidating(false);
try {
saxfac.setFeature("http://xml.org/sax/features/validation", false);
saxfac.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
saxfac.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
saxfac.setFeature("http://xml.org/sax/features/external-general-entities", false);
saxfac.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
}
catch (Exception e1) {
e1.printStackTrace();
}
Answered by: Haris405 | Posted: 01-03-2022
Answer 3
I had this problem before. I solved it by downloading and storing a local copy of the DTD and then validating against the local copy. You need to edit the XML file to point to the local copy.
<!DOCTYPE root-element SYSTEM "filename">
Little more info here: http://www.w3schools.com/dtd/dtd_intro.asp
I think you can also manually set some sort of validateOnParse property to "false" in your parser. Depends on what library you're using to parse the XML.
More info here: http://www.w3schools.com/dtd/dtd_validation.asp
Answered by: Steven114 | Posted: 01-03-2022Similar questions
Disable popup about using higher Java runtime for applet
We are running a java applet and are specifying the java runtime version that we want the applet to use but if the user have a higher version installed they will get a question if they would like to use the higher version instead to load the applet.
Is there a way to disable this question?
We want to do this because we are relaying on changes done in the configuration of the specific version of the java runtime.
java - Disable tap clicks
When on a Tablet PC, touching the screen with your pen is interpreted as a click. The sensitivity is adjusted so that even if your pen slips a little bit, it is still interpreted as a click; you have to move a certain distance before it becomes a drag-n-drop event.
In a notebook style application, however, you never want to "click" inside of the area; the only events that one cares about are mousedown and mouseup.
java - Disable Axis log4j logging in jboss
I have small application that is uploading pictures to another website via webservice.
My current problem is, that Axis is logging the whole xml message (including the binary data of the picture!) via STDOUT and I can't seem to figure out, how to disable it.
My log4j settings for jboss (jboss-log4j.xml) includes an appender for normal STDOUT Info loggings, and I tried to disable axis with different category setting...
java - Disable default ALT key action in JFrame under Windows
I would like to let my JFrame under Windows not act upon an ALT key press. To clarify, when you execute the following snippet of code:
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fra...
java - How to disable the debug logging of class using log4j?
In my log4j.xml, i am having
<logger name="java.sql">
<level value="DEBUG" />
</logger>
But using this, it also enables the logging for the interface Resultset. If i want to disable the logging for this interface but still want to have the debug logging for statement, preparedstatement, etc... How can i do this?
java - Disable back button in GWT
Is there a way to disable the Back button in a browser (basically clearing the History token stack) in GWT? Once I browse to a certain page in my application I want to make sure that the user can't use the back button to go back, but only be able to use links on the page to navigate the site.
is it is possible to disable the windows keys using java
is it is possible to disable the windows keys and alt+tab using java ...
java - Enable or disable column based on other column
I have Application with 3 columns
Result - Radio button pass and Fail
IF Fail - Radio Button Minor and Major
Be Specific - Multiple Choice - Active X control/ Browser/ Display / Others
I want to disable If fail and Be specific if Result is selected pass.
How can I do it
Thanks
Kundan
Java Swing - How to disable a JPanel?
I have several JComponents on a JPanel and I want to disable all of those components when I press a Start button.
At present, I am disabling all of the components explicitly by
component1.setEnabled(false);
:
:
But Is there anyway by which I can disable all of the components at once? I tried to disable the JPanel to which these components are ...
java - How to disable log4j in 3rd party jar?
I'm experimenting writing a java SE swing application using JBoss weld. Weld configures logging with log4j using the following log4j.xml file in the jar:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!--
JBoss, Home of Professional Open Source
Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
contributors by the @author...
iText Java disable print pdf
We are using the following code to disable Print option in PDF. Works really well.
PdfReader reader = new PdfReader("my-old-file.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("my-new-file.pdf"));
stamper.setEncryption("my-owner-password".getBytes(), "my-user-password".getBytes(),
PdfWriter.AllowCopy, PdfWriter.STRENGTH40BITS);
stamper.close();
...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)