How to get all element names with JDOM 2 via XPath in Java 7? - xpath-2.0

I have a XML file like the following.
<?xml version="1.0" encoding="UTF-8"?>
<Site>
<Name>MySite</Name>
<Groups>
<Default></Default>
<GroupA></GroupA>
</Groups>
</Site>
I want to get all names of Groups' children elements (in this example 'Default' and 'GroupA') as a list of strings. I was trying the following.
public List<String> getGroupNames() {
List<String> names = new ArrayList<String>();
XPathExpression<Text> xpath = XPathFactory.instance().compile(
"/Site/Groups/*/name()", Filters.text());
List<Text> elements = xpath.evaluate(document);
if (elements.size() > 0) {
for (Text text : elements) {
names.add(text.getText());
}
return names;
}
return null;
}
This fails hard with the following exception.
class org.jaxen.saxpath.XPathSyntaxException: /Site/Groups/*/name(): 20: Expected node-type
What is the proper syntax for that? I do not understand the exception.

It seems the XPathFactory instance you use only supports XPath 1.
You need to load a library supporting XPath 2, before being able using xpath 2. E.g: with SAXON:
import net.sf.saxon.om.NamespaceConstant;
String objectModel = NamespaceConstant.OBJECT_MODEL_SAXON;
System.setProperty("javax.xml.xpath.XPathFactory:"+objectModel, "net.sf.saxon.xpath.XPathFactoryImpl");

Related

Use of SerializationFeature.WRAP_ROOT_VALUE ahead of xmlRootElement

I just want to understand what is the use of SerializationFeature.WRAP_ROOT_VALUE.
I have actually tried disabling the SerializationFeature.WRAP_ROOT_VALUE and for the class I have annotated with xmlRootElement. Here in this case after disabling the SerializationFeature.WRAP_ROOT_VALUE still after serializing I am getting the root value. To just avoid the root value I have to use the xmlType.
So trying to understand then what is the use of SerializationFeature.WRAP_ROOT_VALUE?
Sample code which I have tried
#XmlRootElement(name="person")
Public class Person {
#XmlElement(name = "insert")
private int insert;
#XmlElement(name = "update")
private int update;
}
The above is the POJO class which I was trying to serialize and also I have used
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
So with the above code the output is
"person" {
"insert" : 1,
"update" : 0
}
In the same case if I try to change the xmlRootElement to XmlType in Person class the output is
{
"insert" : 1,
"update" : 0
}
So I am confused like what is the use of SerializationFeature.WRAP_ROOT_VALUE if it is not giving the expected output?
I am using the Jackson version of 2.9.6
After digging more into this found that with the help of CXF I was able to solve this by adding small config in applicationcontext.xml file
<bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
<property name="dropRootElement" value="true" />
</bean>

WIX: don't show build number from [ProductVersion]

My Wix installer has the product version set like this:
<Product Version="4.6.0.0" ..>
And I show it in the UI like this:
<String Id="WelcomeDlgTitle">{\WixUI_Font_Bigger}Welcome to the [ProductName] v[ProductVersion] Setup Wizard</String>
This works well but I would like to omit the build number (ie. v4.6.0 instead of v4.6.0.0). Is there a way to do this?
You can omit the 4th field of the Product/#Version attribute as Windows Installer ignores it anyway.
<Product Version="4.6.0" ..>
If you really want to keep the 4th field of the version number, you'd have to write a custom action to parse the string and strip the 4th field.
Thank you #zett42, your comment pointed me out in the right direction - I created a Wix Preprocessor Extension, following the documentation here: http://wixtoolset.org/documentation/manual/v3/wixdev/extensions/extension_development_preprocessor.html
Here are the classes I created:
public class MaterialiserExtension : WixExtension
{
private MaterialiserPreprocessorExtension preprocessorExtension;
public override PreprocessorExtension PreprocessorExtension
{
get
{
if (this.preprocessorExtension == null)
{
this.preprocessorExtension = new MaterialiserPreprocessorExtension();
}
return this.preprocessorExtension;
}
}
}
public class MaterialiserPreprocessorExtension : PreprocessorExtension
{
private static string[] prefixes = { "versionUtil" };
public override string[] Prefixes { get { return prefixes; } }
public override string EvaluateFunction(string prefix, string function, string[] args)
{
string result = null;
switch (prefix)
{
case "versionUtil":
switch (function)
{
case "ToString":
if (0 < args.Length)
{
result = Version.Parse(args[0]).ToString(args.Length >1 && args[1] != null ? int.Parse(args[1]) : 4);
}
else
{
result = String.Empty;
}
break;
}
break;
}
return result;
}
}
One thing that was not immediately obvious - in my Visual Studio Wix project, I had to add
-ext "$(SolutionDir)Materialiser.Wix\bin\Release\Materialiser.Wix.dll" to Properties (Alt+Enter) > Tool Settings > Compiler (candle) and the same in Linker (light).
To use, it is very simple:
in my .wxs file I define a property like this:
<?define ProductVersion="4.6.0.5" ?>
Then I use it in Product like this:
<Product Version="$(var.ProductVersion)" .. >
And then I create anew property like this:
<Property Id="VersionWithNoBuildNumber" Value="$(versionUtil.ToString([ProductVersion], 3))" />
and in my .wxl file I use it normally:
<String Id="WelcomeDlgTitle">{\WixUI_Font_Bigger}Welcome to the [ProductName] v[VersionWithNoBuildNumber] Setup Wizard</String>
I hope this helps someone else too :p

Get Jackson XMLMapper to read root element name

How do I get Jackson's XMLMapper to read the name of the root xml element when deserializing?
I am deserializing input XML to generic Java class, LinkedHashMap and then to JSON. I want to dynamically read the root element of input XML on deserialization to LinkedHashMap.
Code
XmlMapper xmlMapper = new XmlMapper();
Map entries = xmlMapper.readValue(new File("source.xml"), LinkedHashMap.class);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writer().writeValueAsString(entries);
System.out.println(json);
Input XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<File>
<NumLeases>1</NumLeases>
<NEDOCO>18738</NEDOCO>
<NWUNIT>0004</NWUNIT>
<FLAG>SUCCESS</FLAG>
<MESSAGE>Test Upload</MESSAGE>
<Lease>
<LeaseVersion>1</LeaseVersion>
<F1501B>
<NEDOCO>18738</NEDOCO>
<NWUNIT>0004</NWUNIT>
<NTRUSTRECORDKEY>12</NTRUSTRECORDKEY>
</F1501B>
</Lease>
</File>
Actual Output
{"NumLeases":"1","NEDOCO":"18738","NWUNIT":"0004","FLAG":"SUCCESS","MESSAGE":"Test Upload","Lease":{"LeaseVersion":"1","F1501B":{"NEDOCO":"18738","NWUNIT":"0004","NTRUSTRECORDKEY":"12"}}}
Expected Output (Note: There is a root element named "File" present in JSON)
{"File":{"NumLeases":"1","NEDOCO":"18738","NWUNIT":"0004","FLAG":"SUCCESS","MESSAGE":"Test Upload","Lease":{"LeaseVersion":"1","F1501B":{"NEDOCO":"18738","NWUNIT":"0004","NTRUSTRECORDKEY":"12"}}}}
There's probably some switch somewhere to set it. Any help shall be appreciated.
Sadly there is no flag for that. It can be done with a custom implementation of com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer. (Jackson How-To: Custom Deserializers):
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser;
import java.io.File;
import java.io.IOException;
//...
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.registerModule(new SimpleModule().addDeserializer(JsonNode.class,
new JsonNodeDeserializer() {
#Override
public JsonNode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String rootName = ((FromXmlParser)p).getStaxReader().getLocalName();
return ctxt.getNodeFactory()
.objectNode().set(rootName, super.deserialize(p, ctxt));
}
}));
JsonNode entries = xmlMapper.readTree(new File("source.xml"));
System.out.println(entries);
The accepted answer works for Jackson 2.10.* (and older probably), but not for any of the newer versions (might get fixed in 2.14 - source).
What worked for me:
public class CustomJsonNodeDeserializer extends JsonNodeDeserializer {
#Override
public JsonNode deserialize(JsonParser p, DeserializationContext context) throws IOException {
//first deserialize
JsonNode rootNode = super.deserialize(p, context);
//then get the root name
String rootName = ((FromXmlParser)p).getStaxReader().getLocalName();
return context.getNodeFactory().objectNode().set(rootName, rootNode);
}
}
I will update my answer if there's a new better solution.
While this Question has an accepted answer, I found that it doesn't work on the latest Jackson version 2.13.2 and uses flawed approach anyway.
new SimpleModule().addDeserializer(JsonNode.class,
new JsonNodeDeserializer() {
#Override
public JsonNode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String rootName = ((FromXmlParser)p).getStaxReader().getLocalName();
return ctxt.getNodeFactory()
.objectNode().set(rootName, super.deserialize(p, ctxt));
}
}));
The .getLocalName() call will return the name of the first child element, not the actual root of the parsed input. Also, fetching just the name of the element ignores the attributes, so you'll end up with just a duplicated tag name in your output.
What to do instead?
After trying a number of workarounds, I've found only one that works properly. You have to let Jackson do its root node removal and fool it with a dummy wrapper tag.
JsonNode jsonNode = XML_MAPPER.readTree("<tag>" + nestedXmlString + "</tag>");
This will wrap the XML with a dummy <tag> which is then immediately removed and forgotten.
Then, you can work with the output tree as usual:
toXmlGenerator.writeTree(jsonNode);
Caution
However, please be aware that if your XML input String contains the XML Header declaration (<?xml...), then wrapping it with a dummy tag will result in a parsing exception. To avoid this, you'll have to first remove the declaration string from the input:
String nestedXmlString = input;
if (nestedXmlString.startsWith("<?xml")) {
nestedXmlString = nestedXmlString.substring(nestedXmlString.indexOf("?>") + 2);
}

Finding namespace of a given Element using VTD-XML

For my application I only need to determine the namespace of the root node, so ideally I would like to execute a single operation to get this namespace.
I have code that uses an XPath to get all of the namespaces declared (/*/namespace::*), but then I have to go through them all and do vn.toRawString for each one to compare it to my prefix, which I would like to avoid.
I'm sure I'm missing something obvious here, but what is the best way to get the namespace of a given element, since xmlns: attributes are not considered in getAttrValNS ?
String elementName = vn.toRawString(vn.getCurrentIndex());
String prefix = prefix(elementName);
String localName = localName(elementName);
QName rootQName;
if (prefix == null) {
rootQName = new QName(localName);
} else {
int nsIndex = vn.getAttrValNS("xmlns", prefix);
// Can't find index because xmlns attributes are not included
String namespace = vn.toRawString(nsIndex);
rootQName = new QName(namespace, localName);
}
Is there a simple XPath that will just give me the namespace of the root node, not ALL namespaces DECLARED on the root node?
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("namespace-uri(.)");
String ns = ap.evalXPathToString();

XInclude support in Java 6

I've seen this example posted several times:
public class XIncludeTest {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setXIncludeAware(true);
DocumentBuilder docBuilder = factory.newDocumentBuilder();
System.out.println("isXIncludeAware " + docBuilder.isXIncludeAware());
Document doc = docBuilder.parse(args[0]);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
}
}
I pass this simple xml file in:
a.xml
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2003/XInclude">
<xi:include href="b.xml" xpointer="element(/b/c)"/>
<xi:include href="b.xml" xpointer="element(/b/d)"/>
</a>
b.xml
<?xml version="1.0" encoding="UTF-8"?>
<b>
<c>1</c>
<d>2</d>
</b>
And all I get back out is the contents of a.xml, as above - no part of b.xml was included. I've tried endless variations on the xpointer syntax to no avail. I have, however, been able to get things to work in perl via XML::LibXML but I need this to work in Java.
What am I not getting?
OK, now I've updated my xml file to something that works:
a.xml
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="b.xml" xpointer="element(/1/1)"/>
<xi:include href="b.xml" xpointer="element(/1/2)"/>
</a>
I'd rather not use offsets into the document - names are much better (like in the first version of a.xml). I'm trying to understand XPointer Framework and have been using XPath and XPointer/XPointer Syntax as a reference as well - it seems I should be able to use a "shorthand" pointer but only if the NCName matches some ID-type attribute. Problem is that I don't have a DTD or schema that defines an "ID-type attribute". Given that the Java parser doesn't support the xpointer schema, is my only option to use indexes?
The namespace for the xi prefix should be "http://www.w3.org/2001/XInclude" (note 2001 not 2003). See http://www.w3.org/TR/xinclude/ for more details.
That gets you to what seems to be the next problem: the syntax of your element scheme xpointer is incorrect. See http://www.w3.org/TR/xptr-element/ for more information.