2

charactersI am trying to include the correct characters in an XML document text node:

Element request = doc.createElement("requestnode");
request.appendChild(doc.createTextNode(xml));
rootElement.appendChild(request);

The xml string is a segment of a large xml file which I have read in:

Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("rootnode");
doc.appendChild(rootElement);

<firstname>John</firstname>
<dateOfBirth>28091999</dateOfBirth>
<surname>Doe</surname>

The problem is that passing this into createTextNode is replacing some of the charters:

&lt;firstname&gt;John&lt;/firstname&gt;&#13;
&lt;dateOfBirth&gt;28091999&lt;/dateOfBirth&gt;&#13;
&lt;surname&gt;Doe&lt;/surname&gt;&#13;

Is there any way I can keep the correct characters (< , >) in the textnode. I have read about using importnode but this is not correctly XML, only a segment of a file.

Any help would be greatly appreciated.

EDIT: I need the xml string (which is not fully formatted xml, only a segment of an external xml file) to be in the "request node" as I am building XML to be imported into SOAP UI

Daniel Flannery
  • 1,166
  • 8
  • 23
  • 51
  • For the sake of data integrity I would always go for a scheme in which I actually parse the XML from the string guaranteeing that it's self contained (e.g. all start tags have a corresponding end tag etc.) For this http://stackoverflow.com/questions/807418/simplest-way-to-query-xml-in-java might help – Oncaphillis Nov 06 '14 at 13:43

1 Answers1

2

You can't pass the element tag and text to the createTextNode() method. You only need to pass the text. You need then to append this text node to an element.

If the source is another XML document, you must extract the text node from an element and insert it in to the other. You can grab a Node (element and text) and try to inserted as a text node in the other. That is why you are seeing all the escape characters.

On the other hand, you can insert this Node into the other XML (if the structure is allowed) and it should be just fine.

In your context, I assume "request" is some sort of Node. The child element of a Node could be another element, text, etc. You have to be very specific.

You can do something like:

Element name = doc.createElement("name");
Element dob = doc.createElement("dateOfBirth");
Element surname = doc.createElement("surname");

name.appendChild( doc.createTextNode("John") );
dob.appendChild( doc.createTextNode("28091999") );
surname.appendChild( doc.createTextNode("Doe") );

Then you can add these element to a parent node:

node.appendChild(name);
node.appendChild(dob);
node.appendChild(surname);

UPDATE: As an alternative, you can open a stream to a document and insert your XML string as a byte stream. Something like this (untested code, but close):

String xmlString = "<firstname>John</firstname><dateOfBirth>28091999</dateOfBirth><surname>Doe</surname>";

DocumentBuilderFactory fac = javax.xml.parsers.DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fac.newDocumentBuilder();
Document newDoc = builder.parse(new ByteArrayInputStream(xmlString.getBytes()));
Element newElem = doc.createElement("whatever");
doc.appendChild(newElem);

Node node = doc.importNode(newDoc.getDocumentElement(), true);
newElem.appendChild(node);

Something like that should do the trick.

hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • Hi, Thanks for your answer. I have updated my question with more detail. The real problem I am having is keeping the (< and >) characters in my xml string when it is passes to createTextNode. – Daniel Flannery Nov 06 '14 at 14:09
  • Hi, I had been looking into parsing the string as a byte stream as you mentioned. It is giving out about the structure of the xml in the string however. "[Fatal Error] :1:3: The markup in the document preceding the root element must be well-formed." Is there any way to ignore the XML formatting and just parse in the string as text ? – Daniel Flannery Nov 06 '14 at 15:37
  • Think about what you are asking. A malformed XML file will not be able to be parsed as a DOM document. Basically what you have is a text file that "kind of" looks like an XML file. You will be losing the power that the XML spec provides. You need to make sure the final XML document is well-formed. Well-formedness is related to the basic rules of XML: Every tag has a closing tag, close all sub-elements before closing the parent, element names can't start with a number, etc. Find out what is breaking this rule and fix it. If you want, update your post with the generated XML string. – hfontanez Nov 06 '14 at 17:37
  • Try again. I just realized the XML string in my example was missing the '>' in the date of birth closing bracket. Sorry. If the answer I provided you fixes your problem, don't forget to mark it as such. Good luck! – hfontanez Nov 06 '14 at 17:40
  • Thank you, @Alexandre Germain. – hfontanez May 07 '19 at 21:30