How to make an XML document subtree to a new ElementTree with XML? - parsing

Imagine an XML as ElementTree object has many branches. Is it possible to take one out as a separate ElementTree?

Related

Merging JSON-LD results with original JSON

I'm working on visualizing several geojson files with a large set of properties. I would like to use json-ld to add some meaning to some of these properties. I don't have a lot experience with JSON-LD, but sucessfully applied the jsonld.js to expand, compact, etc. my geojson file and #context. In doing so I noticed that the end results only returns the graph that is actually described in the context. I can understand that, but since it only represents a small part of all my properties, I have some difficulty using the results.
It would help me if I could somehow merge the results of the jsonld operation with the original geojseon file. eg:
"properties": {
"<http://purl.org/dc/terms/title>": "My Title",
"<http://purl.org/dc/terms/type>": "<http://example.com/mytype>",
"NonJSONLDPropertyKey" : "NonJSONLDPropertyValue",
etc.
I would still be able to recognize the properties with an URI, but could also work with the non-json-ld properties. Any suggestions how this might work? Or is there a better approach?
You could map all other properties to blank nodes... that is identifiers that are scoped to the document. The simplest way to do so is to add a
"#vocab": "_:"
declaration to your context.

How to prohibit DTD entities

I'm using the XML Tree API and the XML Parser API in c++, and I would like to prohibit entities creation in my XML documents.
What is the best way to do that when using these API's?
I've only seen examples of how to prohibit DTD entities when using XML Reader, and none when using the XML Tree or Parser API.
Thanks!
When using the tree API, you can call xmlGetIntSubset and inspect the xmlDtd structure to check whether a document contains entity declarations. When using the SAX parser, you can register an entityDeclSAXFunc callback.

Ant task for generating XSD from POJOs

Is there an ant task for generating an XSD from POJOs?
I'm presuming that you want to serialize a Java POJO object and then use XSD to validate the serialised XML data, or author new object instances which can instantiated via your de-serialization process.
Thing is Java XML serialization comes in two favours (Examples follow):
Xstream XML is dynamically generated from fixed object class description
XMLBeans Java classes dynamically generated from fixed XML Schema
Now perhaps you're using something else that combines both approaches?
What I'd recommend is create (or generate) an XSD based on the XML your object creates when serialized. Relatively speaking Java objects don't change that often and when they do a far great challenge is supporting multiple versions (Reading data encoded for the older version of your object). To address this challenge I'd recommend reading the following article for one possible solution:
http://java.dzone.com/articles/migrate-serialized-java

smartgwt beginner- how to display xml entered in text area into tree

I am working with Smart GWT 3.0 LGPL- I am trying to display in a tree the entire block of XML entered by user into a text area.
I can get the nodelist from the xml entered by user, however in Smart GWT, Tree widget accepts only TreeNode[] members as data.
How do I convert the NodeList (obtained using GWT's inbuilt XML parser) into TreeNode[]? Is my approach correct(to display the xml in tree form)? Or is the solution to solve this problem completely different from what I am thinking?
I assume you use the SmartGWT's TreeGrid object to create the tree. The TreeGrid is just a specialized ListGrid and, as the later, it can be databounded to a Datasource, which can easily parse an XML. Look at this example. It should help you to create a solution to your problem. You can just show only one field in your TreeGrid, if all you need is a simple tree.
In the case that you can't access the xml data through a URL, like accessing them through your text area, after you have parsed them as a NodeList, you should iterate them and create an array of TreeNode objects. For each Something object you should set its associated TreeNode object's attributes using the setAttribute(attributeName, attributeValue) methods. Then create a Tree object, and starting from the root you can add the TreeNode objects using the various methods of the Tree, at the required position. Then a simple: treeGrid.setData(Tree tree) will load and show your data in your TreeGrid.
You can create an array TreeNode[] which have size as nodelist's length, and in a cycle to put on a value. And after that you can set in a tree this TreeNode[].

In Rails should objects be limited to those that will have a view and controller?

I've been struggling with a design problem, and I admit that I am new to both OOP and RoR so I'm sure this is going to be very basic. I have an application where I am reading from text files in various formats in order to parse information related to hands of poker. So what I have are three entities:
A file object. This stores the name and path of the file and some other attributes, and has functions related to reading from the files. This is MVC because I can add a file and have it be auto-updated, or I can just parse the info from a file on the fly.
The poker hand object. This essentially just stores information about who played the hand of poker and what the actions and results were.
A parser. This reads external JSON files with different regex patterns depending on the type of file that is being read. It also has some basic state machine info in the JSON file so that alot of the logic is removed from the parser.
So my initial feeling about the parser was that it should be its own object. But then I realized that it didn't have a V or a C and so possibly didn't fit with the Rails way of doing things. And it also doesn't have any functionality that is needed by any object other than the file object, and so seemed to fit within the file. But at the same time it's so distinctly different than a file object, that it didn't seem to fit. I thought of a module, but the point of modules seems to be if multiple objects share the need for some functions, and in this case only the file does.
So should it be its own object, be within the file object, or is there some other alternative I'm not seeing?
The decision about whether something should be an "M" in MVC should be based on whether it has any persistent (database-driven) data.
Models don't need a controller or views, and controllers don't have to map one-to-one with models. However, the common "RESTful API" approach does result in a strong model <=> controller correspondence.
In your case it sounds like it's just a chunk of code which takes input and returns some other already-defined model, so it probably sits best as a module in your lib/ folder which you can call from some of your other models or controllers
But then I realized that it didn't have a V or a C and so possibly didn't fit with the Rails way of doing things.
The fact that it doesn't have a V or C is irrelevant in my opinion.
If you feel like the parser belongs with the File, then stick it there. But if you don't (and to me it doesn't sound like it), it's perfectly OK to stick it in it's own class. There is no need that all Models have associated Controllers and Views, nor that they derive from ActiveRecord::Base or any other ORM, nor have anything to do with the database whatsoever.
Regarding whether it belongs in lib or app/models - I look at it like this:
If it's part of your app, it belongs in app/models. If it's not part of your app, like an external library, don't put it in app/models - put it in the lib folder.
It sounds like your parser should be a utility class, rather than part of the model itself. Think of it this way: the model should comprise all of the logic which your application needs to do its job. The parser's job is to get external data into a format which that logic can work on; it's not part of the logic itself.
I'd keep your parser outside File, and put it in lib/.
I use to let classes that implement domain-related logic in the models folder. You should be aware that they won't be automaticly reloaded if let in the /lib folder.

Resources