Simple XML Framework - Adding XSI Location in JAVA - simple-framework

Issue: No annotation provided for adding XSI location to root. Another stackoverflow question detailed the same problem but in C#. I'm not quite sure how to covert that over to JAVA. I need to update XML in a flat file and thought of just using string manipulation to add the XSI locations but I was hoping there might be a cleaner approach.
Reference Question: C# Stackoverflow Same Issue
Sample XML:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:MySample xmlns:ns0="someurl" xsi:schemaLocation="someOtherurl">
<othertag/>
</ns0:MySample>

Since it the schemaLocation is an attribute and not a namespace you can simply parse it using #Attribute annotation.
#Attribute
private String schemaLocation;
Do not forget to declare the Namespace of the root element:
#Namespace(prefix="xsi", reference="http://www.w3.org/2001/XMLSchema-instance"),
This worked just fine for me.

Related

support for java.time.Instant serde via jackson JavaTimeModule

I'd like to support serde of POJO classes that include java.time.Instant member fields. As such, I was happy to find a Jackson module that is designed precisely for this use case:
https://github.com/FasterXML/jackson-modules-java8
Unfortunately I am unable to register the JavaTimeModule as follows because it fails to compile given I need to import a flink-shaded jackson2 jar that includes JavaTimeModule but am unable to find it (eg in maven-central):
private ObjectMapper mapper = new ObjectMapper()
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.registerModule(new JavaTimeModule());
Thoughts?
thx,
james
It's not entirely clear how you currently use jackson. But in general, there is no need to include flink-shaded-jackson in your user jar. In fact, it is heavily discouraged. The whole point of shading is that you can use your own version without class conflicts. So simply add jackson with the respective module to your gradle project and use it as is.
Now if you use any given format/connector of Flink that uses the flink-shaded-jackson, then you need to shade the time module in the same fashion, unfortunately. You can use the json schema module as a reference.

Custom template delimiter for kataras/iris framework, Go Language

How do I add a custom delimiter to iris templates? I'm using angularjs for the front end and it conflicts with what angularjs is doing. I found some reference here but when I tried, it doesn't work, looks like the author of iris just gave a simple snippet just to describe it.
I'm quite new to Go so even checking the source code of iris, I'm having trouble. Thanks.
Note: I know that I should be using static files for this, but this just really peaked my curiousity

How do I add CDATA as a value to a particular node without using docptr

I wanted to create and xml file like the one below.
I treated the value of the child node as string and added it using the function xmlNewText(BAD_CAST string) where string is the value I wanted to place in the child node
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<child>![CDATA[<data>hello</data>]]</child>
</Root>
But this is converted to
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<child>![CDATA[<data>hello</data>]]</child>
</Root>
Can anyone guide me in doing so? I have seen some questions and the answers are based on creating cdatasection using the docptr. I need to create the cdatasection without using the docptr. I just have the rootptr with me. Is it possible to do so? I am using c as the programming language and the library which I am using is libxml
You really can't construct XML content by inserting it into the DOM as text. This example shows some of the reasons why -- not only is the DOM correctly escaping the content of the text node, but you've got the syntax of wrong.
Instead, use the node factory to create a CDATASection node containing the text you want to include in the document.
Or -- since there's no good reason to use a CDATA Section here -- create a Text node without trying to manually wrap its contents in , and let the DOM serializer escape it as appropriate. You'll probably get character-by-character escaping, which doesn't look as pretty in the XML file... but it will mean exactly the same thing to any properly-written application processing that XML.
(Admittedly there are some applications which do care about the difference between normal text and CDATA sections. The technical term for those applications is "broken".)

Persistent data for winform c#

I have a web poster application and I want to create a "Definition" file for it. Basically just a bunch of strings I import into the program at startup. I want them in an external source so I can update it without changing the executable.
I was thinking of creating a new static class, say "PosterDefinition", and on the startup of the application import the definition file, and set the PosterDefintion values from there.
As for how I will save it, maybe serialize the data from the program itself (a one time process).
Please share your thoughts.
Thanks.
I would suggest that you save it in an XML file. This would allow you to get the data quickly using LINQ-XML. What you could do is make properties that look at the file for making changes. You could cache values in the Application object. After a few requests, or after each one, check on the file. Because LINQ, or better yet PLINQ, excecutes quickly, the requests would not take long.
Here is an example XML document:
<?xml version="1.0" encoding="utf-8"?>
<Definition>
<Item>
<Title>t</Title>
<Something>Pie</Something>
</Item>
</Definition>
And here is how to access the Something of an item with Title t
using System.Xml;
using System.Xml.Linq;
XElement element = XElement.Load("definitions.xml");
XElement item = (from item in element.Elements()
where item.Elements().Where(i => (string)i.Element("Title") == "t")
select item.Element("Something")).First();
I havn't tested this and I am bad at LINQ so check this.

Why do XML Namespaces usually start with http://...?

For example, the WPF namespace is:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
If I define my own namespace, should it also start with http? Isn't http misleading?
Namespaces doesn't have any meaning in their value, except for that value to be unique to avoid nameclashing. This is why you see a lot of companies putting in the URL for their own website as the namespace. URLs serve the same purpose, to be unique. Over the years it's just become good practice to use an URL, because if everyone does that, no nameclashing should occur :)
The W3C Document defining XML Namespaces says (quoting) :
Definition: An XML namespace is
identified by a URI reference
[RFC3986]
And RFC 3986 says (quoting) :
1.1.1. Generic Syntax
Each URI begins with a scheme name,
as defined in Section 3.1, that
refers to a specification for
assigning identifiers within that
scheme.
So I guess using http:// is what's closest to the standard -- as HTTP is the most common scheme used on the net.
In addition, as there can be only one owner for a domain name, it allows each company to use it's URL in its namespaces.
Another common way instead of using a URL starting with http:// is to use a Uniform Resource Name whose format is defined by RFC2141.
Such namespace identifiers are e.g. used by ODF (OpenDocument Format):
urn:oasis:names:tc:opendocument:xmlns:office:1.0
urn:oasis:names:tc:opendocument:xmlns:style:1.0
urn:oasis:names:tc:opendocument:xmlns:text:1.0
From this article at W3Schools:
"The namespace URI is not used by the parser to look up information. The purpose is to give the namespace a unique name. However, often companies use the namespace as a pointer to a web page containing namespace information. Try to go to http://www.w3.org/TR/html4/."
It is a reliable way to create a readable globally unique identifier. It may or may not be to a valid URL with more information.

Resources