Reputation: 12379
Given:
public static final String XML_POLICY =
"<?xml version="1.0"?>"
+"<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">"
+ "<cross-domain-policy>"
+ "<allow-access-from domain="*" to-ports="*" />"
+ "</cross-domain-policy>";
I am getting compile errors on the
"<?xml version="1.0"?>"
it says ';
' expected.
and on
+"<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">"
it says "cannot find symbol, symbol: class xml and class dtds"
What could be the possible cause? IDE is Netbeans 6.8 Beta.
Upvotes: 0
Views: 339
Reputation: 29730
I think the question is well answered, but just a note/hint:
thanks to syntax highlighting, part of the text should be coloured differently indicating that it's not part of the literal.
Netbeans must have syntax highlighting enabled (default?). Stackoverflow also has syntax highlighting, just look at the code in the question (above).
Upvotes: 0
Reputation: 1503479
You need to escape the double quotes - and it's not just the first line, either. The doctype and allow-access-from lines require it too.
// Reformatted slightly to avoid scrolling :)
public static final String XML_POLICY =
"<?xml version=\"1.0\"?>"
+ "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">"
+ "<cross-domain-policy>"
+ "<allow-access-from domain=\"*\" to-ports=\"*\" />"
+ "</cross-domain-policy>";
An alternative it to use single quotes within the XML, which is fine in most places, and makes the code simpler to read:
public static final String XML_POLICY =
"<?xml version='1.0'?>"
+"<!DOCTYPE cross-domain-policy SYSTEM '/xml/dtds/cross-domain-policy.dtd'>"
+ "<cross-domain-policy>"
+ "<allow-access-from domain='*' to-ports='*' />"
+ "</cross-domain-policy>";
Note that there's nothing Netbeans or XML-specific about this. A double quote is used to indicate the start and end of a string, which is why you need to escape it if you want one in the middle. For example:
String x = "I said, \"Hello.\" The child laughed.";
There are various other escape sequences in Java - see section 3.10.6 of the language specification for more details.
Upvotes: 3
Reputation: 116442
you must escape the " inside the string:
public static final String XML_POLICY =
"<?xml version=\"1.0\"?>" + // etc etc
Upvotes: 2