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
immerhart
  • 1,279
  • 4
  • 20
  • 35

4 Answers4

2

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,051
  • 2
  • 24
  • 34
1

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

AlexR
  • 111,884
  • 15
  • 126
  • 200
  • Or in other words: when parsing an XML document which contains "&#8743;" 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. `&#8743;` gets to `&amp;#8743;` – immerhart 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 – immerhart 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
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
  • 36,099
  • 12
  • 75
  • 145
  • this post is senseless XD. how to pass something other than a string to the `tag.setAttribute()` method than a string? – immerhart 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