<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Xml on MarkJacobsen.net</title><link>https://test.markjacobsen.net/tags/xml/</link><description>Recent content in Xml on MarkJacobsen.net</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Mon, 25 Mar 2013 07:25:27 +0000</lastBuildDate><atom:link href="https://test.markjacobsen.net/tags/xml/index.xml" rel="self" type="application/rss+xml"/><item><title>XPath Query Not Returning any Results in Java</title><link>https://test.markjacobsen.net/2013/03/xpath-query-not-returning-any-results-in-java/</link><pubDate>Mon, 25 Mar 2013 07:25:27 +0000</pubDate><guid>https://test.markjacobsen.net/2013/03/xpath-query-not-returning-any-results-in-java/</guid><description>&lt;p&gt;In looking over documentation on XPath queries it seemed easy enough to be able to pull out values from XML, but try as I might I kept getting no results back. Even searching on the root element “/elementName” didn’t return any results. Of course, as with all things Java there’s a trick and that trick is to call setNamespaceAware(). Here’s a quick and dirty example…&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(false); // NEVER FORGET THIS
DocumentBuilder builder = domFactory.newDocumentBuilder();
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(&amp;quot;/items/item[@id='2']&amp;quot;);
return (NodeList)expr.evaluate(domDocument, XPathConstants.NODESET);
&lt;/code&gt;&lt;/pre&gt;</description></item><item><title>Output a DOM Document as XML using Java</title><link>https://test.markjacobsen.net/2013/03/output-a-dom-document-as-xml-using-java/</link><pubDate>Sat, 23 Mar 2013 02:23:28 +0000</pubDate><guid>https://test.markjacobsen.net/2013/03/output-a-dom-document-as-xml-using-java/</guid><description>&lt;p&gt;Ok, here’s another nerdy post for all the java developers out there.  Hopefully it saves you some time.&lt;/p&gt;
&lt;p&gt;Let’s say you have a XML DOM Document that you’ve been modifying in your code, and now want to write it back out as XML.  One would think there would be a nice toXml() method or something on the Document, but there isn’t.  Instead, make sure you have the xalan.jar file on your class path and use this function…&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public static String getDomDocumentAsXml(org.w3c.dom.Document domDocument)
{
	try
	{
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, &amp;quot;yes&amp;quot;);
		StreamResult xmlOutput = new StreamResult(new StringWriter());
		transformer.transform(new DOMSource(domDocument), xmlOutput);
		return xmlOutput.getWriter().toString();
	}
	catch (Exception e) { e.printStackTrace(); return null; }
}
&lt;/code&gt;&lt;/pre&gt;</description></item></channel></rss>