How to drive XML generation from XSD and rules? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am looking for a standard technology to drive the generation of an XML document based on an XSD and a set of rules. Basically I have XSDs that tell me what the XML should look like and what elements are mandatory or optional. What is not in the XSDs is a set of business rules that say things like "if such element's value is this, that other element is actually mandatory" or "if such element's value is that, that other element should be omitted".
What I have in mind is something that would process the XSDs along with the rules (maybe expressed in something like XPath) and call back my code to generate the mandatory values. The structure of the final document would change dynamically depending on the values of the elements driving the conditions.
I guess I could do something close to what I want with XSLTs. I'd generate all the values with and then use an XSLT to enforce the conditions. But in my case some values maybe take long to produce so I want to avoid computing unnecessary values, meaning values that will be later discarded by the business rules.
Does such a technology exist? FYI I am coding in Java but I am hoping to find a generic technology if possible.
Cheers,
Tom

The problem you described can probably be handled by Schematron. It can be used with XML Schema, and if you already know XPath and XSLT you won't find it difficult to understand. If can specify complex relationships between unrelated nodes based on values and context beyond the abilities of XML Schema.
The specification and many tutorials you can find in the Schematron website.

Related

Architecture of system [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am a newbie in Java and I have a unique requirement. I am getting xml data from client and I had already defined schema with me. Now, the problem is client side xml has many more element than schema I have. How to deal with it?
Further, there are some elements which have different architecture then what is defined in schema, I want to transform it into form of what schema has. Does it sound wrong since schema is generally there for validating? Can someone give me overview of how to deal with the problem with small example?
The purpose of an XML schema is to describe the structure of a set of XML documents. If your client is sending XML which does not conform to your schema, then one of three things must be true:
The client is incorrect. If so, you should reject their request as it is not valid.
Your schema is incorrect. In this case, you should correct your schema so that it describes exactly what is permitted in requests.
Your schema is describing something else entirely. In this case, you should write another schema which does describe the permitted requests. You may want then want to transform documents matching this schema into documents matching your new schema - XSLT is a great tool for doing exactly this.
I notice you've tagged JAXB too - another way to perform the transformation would be to construct JAXB representations of both schemas and perform the conversion yourself in code.
Is this helpful? I'd say first and foremost, you need to work out what role your XML schema is performing in your system, and that's a design task you should do before you start writing any code.

How can I parse in Java an xml block to string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've an xml like which looks like this one:
<rootElement>
<title> randmonString </title>
<subElement1>
<someInfo> info </someInfo>
<subElemntTrash> trash </subElementTrash>
<someInfo1> info1 </someInfo1>
</subElement1>
<trash>
<subtrash> trash </subtrash>
</trash>
<date> 19.03.15 </date>
</rootElement>
I need to extract only title, some /subElement1/subInfo, /subElement1/subInfo1 and date, rest should be automatically stored somewhere but without those elements, that were already extracted. I also should have possibility to marshal it back to the original xml.
It would be great if it can be done using annotation mapping.
Can someone give me the right direction to search?
You are asking about parsing, but then you want data extraction, data transformation and finally storing in some undefined form. Very broad question with many possible aporaches.
You can parse XML in java using DOM, SAX, StAX.
You can use XPath to extract interesting information, but it will not divide your document into the interesting bit and the 'rest'.
You can define XSLT templates, to initiate java Transformer, in order to split your input document into the interesting and 'the rest' parts.
You can use JAXB to map the xml into an java model (using your favourite the annotation mapping), and then you can build another representations containing your interesting and 'the rest' part. Then you can save both representation to different xml.

Persisting technology agnostic Java Model: JAXB vs. eCore [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Problem
Given a technology specific Java Model T, I want to have:
a technology agnostic model A to use in my code (i.e. the M in MVC).
a persistence mechanism P to store my model A.
Approach
Currently I have figured out some options:
Option 1:
Manually create the classes for A
Manually create the code to translate T to A
Manually create an XSD of a third JAXB-Model J
Generate JAXB-Model-Classes from J
Manually create the code to translate A to J
Use JAXB to automatically save J
When loading all translations have to be created into the other direction manually.
Advantage: can't see one. Thats my question :)
Disadvantage: most developing effort of all Options.
Option 2:
Manually create an XSD of a JAXB-Model of A
Generate JAXB-Model-Classes for A
Manually create the code to translate T to A
Use JAXB to automatically save A
When loading the translations has to be created into the other direction manually.
Advantage: Low developing effort. A can be persisted directly.
Disadvantage: I read, that it's a bad Idea, to use JAXB-classes as modell. But I don't understand why.
Option 3:
Manually create the Model of A as an eCore-Model
Generate the classes of A from the ecore-Model
Manually create the code to translate T to A
Use eCore-resources to automatically save A
When loading the translations has to be created into the other direction manually.
Advantage: Low developing effort. A can be persisted directly.
Disadvantage: None?
Looking at the advantages and disadvatages, I it seems like I should definitly use eCore instead of JAXB.
Question
Is it really that clear, to use eCore? Or are there advantages/disadvantages I should also think about?
Is here an easier way to translate from the technology specific model T to the technology agnostic modell A. Currenly I have to implement A and the translation A <-> T myself. Some easier way would be nice.
You could use JAXB to convert your Model A to XML. This leverages the Java first aspect of JAXB instead if the schema first in your question.

Turn HTML into XML and parse it -- Android Apps [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have been learning how to build android apps this summer. I am currently trying to work on xml parsing which falls under java in this case. I have a few questions that are mostly conceptual and one specific one.
First, in most of the examples I have seen pages already in xml are used. Can I use a page in regular html format and with whatever the program does turn it to xml and then parse it? Or is that what is normally done anyway?
Secondly, I could use a little explanation on how the parser actually works and saves the data so I will better know how to use it (extract it from whatever it is saved in), when the parsing is done.
So for my specific example I am trying to work with some weather data from the NWS. My program will take the data from this page, and after some user input take you to a page like this, which sometimes will have various alerts. I want to select certain ones. This is what I could use help with. I haven't really coded anything on that yet because I don't know what I am doing.
If I need to clarify or rephrase anything in here I am happy too and let me know. I am trying to be a good contributor on here!
Yes you can parse HTML and there are many parsers available too, there is a question about it here Parse HTML in Android, then we have an answer here about parsing html https://stackoverflow.com/a/7114346/826657
Although its a bad idea, as the tag names aren't well named, so you will have to write lots of code searching attributes for a specific data tag, so you always have to prefer XML,for saving lots of code space and also time.
Here is a text from CodingHorror which says at general parsing html is a bad idea.
http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html
Here is something which explains parsing an XML document using XML PullParser http://www.ibm.com/developerworks/library/x-android/

What is the rationale behind Apache Jena's *everything is an interface if possible* design philosophy? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
If you are familiar with the Java RDF and OWL engine Jena, then you have run across their philosophy that everything should be specified as an interface when possible. This means that a Resource, Statement, RDFNode, Property, and even the RDF Model, etc., are, contrary to what you might first think, Interfaces instead of concrete classes.
This leads to the use of Factories quite often. Since you can't instantiate a Property or Model, you must have something else do it for you -- the Factory design pattern.
My question, then, is, what is the reasoning behind using this pattern as opposed to a traditional class hierarchy system given the nature of the content the library aims to serve? It is often perfectly viable to use either one. For example, if I want a memory backed Model instead of a database-backed Model I could just instantiate those classes, I don't need to ask a Factory to give me one.
As an aside, I'm in the process of writing a library for manipulating Pearltrees data, which is exported from their website in the form of an RDF/XML document. As I write this library, I have many options for defining the relationships present in the Peartrees data. What is nice about the Pearltrees data is that it has a very logical class system: A tree is made up of pearls, which can be either Page, Reference, Alias, or Root pearls.
My question comes from trying to figure out if I should adopt the Jena philosophy in my library which uses Jena, or if I should disregard it, pick my own design philosophy, and stick with it.

Categories