0

Possible Duplicate:
How to disable/avoid Ampersand-Escaping in Java-XML?

i need to create something like: ∧(in the xml-file) and the problem is, that the java technique i am using convertes it to ∧ what doesn´t work for me. i need it in the first format. so, the question is, is there a way to escape it in some way or whatever to get it like that: ∧? for the exporting i am using the same method as here: link

Community
  • 1
  • 1
  • 1
    Please show us your code fragment, and we'll help you correct it. – Sergey Kalinichenko Nov 06 '12 at 15:28
  • i am using the method i have linked. -__- this project is big. but it works in the same way as on the linked page. i am using this: `tag.setAttribute("defaultText", "∧");` –  Nov 06 '12 at 15:41

4 Answers4

1

The way to do this is using &. Typically it works. If it does not work for your please post some code snippets.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Or in other words: when parsing an XML document which contains "∧" with a proper XML parser will return "∧" again. – Puce Nov 06 '12 at 15:35
  • now it doesn´t work. if it would work, i wouldn´t ask. `∧` gets to `∧` –  Nov 06 '12 at 15:36
  • And yet in other words: an XML document which contains "∧" is not a valid XML document, AFAIK. – Puce Nov 06 '12 at 15:37
  • Please show the relevant code, as suggested. – Puce Nov 06 '12 at 15:38
  • how the user should enter the sign that is not on the keyboard? `∧` is valid xml –  Nov 06 '12 at 15:48
  • Yes, you are correct, it's valid: http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-references Sorry for the confusion. – Puce Nov 06 '12 at 16:00
1

Is this what you want?

    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = f.newDocumentBuilder();

    Document d = builder.newDocument();
    Element root = d.createElement("root");
    d.appendChild(root);
    root.setTextContent("this text contains the \u2227 character");

    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.ENCODING, "US-ASCII");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(new DOMSource(d), new StreamResult(System.out));

which produces

<?xml version="1.0" encoding="US-ASCII" standalone="no"?>
<root>this text contains the &#8743; character</root>
forty-two
  • 12,204
  • 2
  • 26
  • 36
0

I think the issue is that you're trying to encode the XML by hand.

Try:

  • Convert the user input to Java String
  • Let the XML library take care of converting from Java String to XML String
Puce
  • 37,247
  • 13
  • 80
  • 152
  • this post is senseless XD. how to pass something other than a string to the `tag.setAttribute()` method than a string? –  Nov 06 '12 at 19:49
  • @immerhart according to forty-two's response, "∧" in XSD is equal to "\u2227" in Java. So, make sure you use the Java character encoding, not the XSD character encoding. – Puce Nov 07 '12 at 08:46
0

You could try:

Document doc = documentBuilder.newDocument();
Element root= doc.createElement("root");
doc.appendChild(root);

Document newDoc = documentBuilder.parse(new InputSource(new StringReader("<element defaulttext=\"&#8743;\">some text or XML</element>")));

Element newElement = newDoc.getDocumentElement();
Node node = doc.importNode(newElement, true);

root.appendChild(node);
siwest
  • 66
  • 7