When I run the attached Junit test, I get the output below. The 
"unrecognized property" appears to be the null at the end of the Java 
string into which the object was serialized.  (NOTE: this is a spike to see 
if serialization code using an obsolete javax.xml class [OutputFormat] can 
be replaced with XmlMapper.)  Am I misusing XmlMapper here? 

<?xml version="1.0" encoding="UTF-8"?><Companies 
xmlns="https://crunchify.com/TestXMLdom";>
<Company id="1">
<Name>Paypal</Name>
<Type>Payment</Type>
<Employees>1000</Employees>
</Company>
<Company id="2">
<Name>eBay</Name>
<Type>Shopping</Type>
<Employees>2000</Employees>
</Company>
<Company id="3">
<Name>Google</Name>
<Type>Search</Type>
<Employees>3000</Employees>
</Company>
</Companies>

XML DOM Created Successfully..
Serialized length: 487
Serialized length without whitespage: 481

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 
Unrecognized field "" (class netmedical.util.TestXMLdom), not marked as 
ignorable (0 known properties: ])
 at [Source: (StringReader); line: 1, column: 488] (through reference 
chain: netmedical.util.TestXMLdom[""])

at 
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62)
.....

Process finished with exit code 255

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.
package netmedical.util;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;

import static netmedical.util.TestXMLdom.createDom;

public class WhenDOMIsSerialized {
	XmlMapper mapper = new XmlMapper();


	@Before
	public void setup() {
	}

	@Test
	public void testDocumentCanBeDeserialized() throws IOException {
		Document originalDom = createDom();
		String serializedDom = mapper.writeValueAsString(originalDom);
		System.out.println("Serialized length: "+ serializedDom.length());
		System.out.println("Serialized length without whitespage: "+ serializedDom.replaceAll("\\s+","").length());
		TestXMLdom deserializedDom = mapper.readValue(serializedDom,
													  TestXMLdom.class);
	}

}

/**
 * Modified version of CrunchifyCreateXMLDOM class
 * @see <a href="https://crunchify.com/java-simple-way-to-write-xml-dom-file-in-java/";>
 *     Java: Simple Way to Write XML (DOM) File in Java
 *     </a>
 */
class TestXMLdom {

	public static Document createDom (){
		DocumentBuilderFactory icFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder icBuilder;
		Document doc = null;
		try {
			icBuilder = icFactory.newDocumentBuilder();
			doc = icBuilder.newDocument();
			Element mainRootElement = doc.createElementNS("https://crunchify.com/TestXMLdom";, "Companies");
			doc.appendChild(mainRootElement);

			// append child elements to root element
			mainRootElement.appendChild(getCompany(doc, "1", "Paypal", "Payment", "1000"));
			mainRootElement.appendChild(getCompany(doc, "2", "eBay", "Shopping", "2000"));
			mainRootElement.appendChild(getCompany(doc, "3", "Google", "Search", "3000"));

			// output DOM XML to console
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource source = new DOMSource(doc);
			StreamResult console = new StreamResult(System.out);
			transformer.transform(source, console);

			System.out.println("\nXML DOM Created Successfully..");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}

	private static Node getCompany(Document doc, String id, String name, String age, String role) {
		Element company = doc.createElement("Company");
		company.setAttribute("id", id);
		company.appendChild(getCompanyElements(doc, company, "Name", name));
		company.appendChild(getCompanyElements(doc, company, "Type", age));
		company.appendChild(getCompanyElements(doc, company, "Employees", role));
		return company;
	}

	// utility method to create text node
	private static Node getCompanyElements(Document doc, Element element, String name, String value) {
		Element node = doc.createElement(name);
		node.appendChild(doc.createTextNode(value));
		return node;
	}
}





Reply via email to