Remove XML Node using java parser

Alternative DOM Approach Alternatively, instead of doing a brute force traversal of the XML document you could use the XPath capabilities in the JDK to find the “B” element with value “13” and then remove it from its parent: import java.io.File; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.*; import org.w3c.dom.*; public class … Read more

XSLT – Comparing preceding-sibling’s elements with current’s node element

Almost correct. <xsl:if test=”preceding-sibling::recurso[1]/unidad != unidad”> </xsl:if> The :: is for axes, not for moving along a path (“making a location step”). In XPath terminology: preceding-sibling::recurso[1]/unidad != unidad ””””””””’ ++++++++++ ++++++ ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ ‘ = axis name (optional, defaults to “child”) + = node test (required) # = predicate (optional, for filtering) ~ = … Read more

Creating an XML document using namespaces in Java

There are a number of ways of doing this. Just a couple of examples: Using XOM import nu.xom.Document; import nu.xom.Element; public class XomTest { public static void main(String[] args) { XomTest xomTest = new XomTest(); xomTest.testXmlDocumentWithNamespaces(); } private void testXmlDocumentWithNamespaces() { Element root = new Element(“my:example”, “urn:example.namespace”); Document document = new Document(root); Element element = … Read more

Converting XML elements to XML attributes using XSLT

This should work: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:template match=”INVENTORY”> <INVENTORY> <xsl:apply-templates/> </INVENTORY> </xsl:template> <xsl:template match=”ITEM”> <ITEM> <xsl:for-each select=”*”> <xsl:attribute name=”{name()}”> <xsl:value-of select=”text()”/> </xsl:attribute> </xsl:for-each> </ITEM> </xsl:template> </xsl:stylesheet>

transformer.setOutputProperty(OutputKeys.ENCODING, “UTF-8”) is NOT working

I had the same problem on Android when serializing emoji characters. When using UTF-8 encoding in the transformer the output was HTML character entities (UTF-16 surrogate pairs), which would subsequently break other parsers that read the data. This is how I ended up solving it: StringWriter sw = new StringWriter(); sw.write(“<?xml version=\”1.0\” encoding=\”UTF-8\” ?>”); Transformer … Read more

Looping through Markers with Google Maps API v3 Problem

You are having a very common closure problem in the following loop: for(x in locations){ console.log(x); infowindow[x] = new google.maps.InfoWindow({content: x}); marker[x] = new google.maps.Marker({title:locations[x][0],map:map,position:locations[x][2]}); google.maps.event.addListener(marker[x], ‘click’, function() {infowindow[x].open(map,marker[x]);}); } Variables enclosed in a closure share the same single environment, so by the time the click callbacks are executed, the loop has run its course … Read more

parse xml response with jQuery

To fix the expected response data type to XML right in your request, set the dataType parameter to “xml”. If you don’t, jQuery uses the response headers to make a guess. It is supported on the $.ajax() function as part of the options object, as well as on $.get() and $.post(): jQuery.ajax( options ) jQuery.get( … Read more

Parsing XML file containing HTML entities in Java without changing the XML

I would use a library like Jsoup for this purpose. I tested the following below and it works. I don’t know if this helps. It can be located here: http://jsoup.org/download public static void main(String args[]){ String html = “<?xml version=\”1.0\” encoding=\”UTF-8\”?><foo>” + “<bar>Some&nbsp;text &mdash; invalid!</bar></foo>”; Document doc = Jsoup.parse(html, “”, Parser.xmlParser()); for (Element e : … Read more