How to get data from XSD file - java

I have an XML schema file. I want to use Java to open this file, find all elements, extract their names, and write them to a text file. I have tried various libraries, but I could not get this to work. Please suggest any libraries or other methods that will work.

XSD files are XML files so you can use any suitable XML processing library to parse it.

I don't think you'll find a library that exists for your purpose, you'll have to write one yourself. The XSD is simply an XML file that conform to the schema:
Traverse the file as normal using DOM or SAX or whatever your preference is.

I assume you are looking for the information conveyed by the XML Schema. I would recommend you the XSOM library, it takes away dealing with the XML itself; nonetheless, a basic understanding of what can be described by an XSD is most likely required.
Follow the user guide, there are some examples showing how to move towards what you need.

Related

How to convert the ConfigProperties_server1.props which is extracted from websphere using wsadmin to xml or json file?

This is the Jython script I have used to extract the ConfigProperties_server1.props file:
AdminTask.extractConfigProperties('[-propertiesFileName ConfigProperties_server1.props -configData Server=server1]')
Welcome to SO. The AdminTask.extractConfigProperties command has no option to control the format of the file. The option PortablePropertiesFile might sound promising, but instead it controls whether internal XMI ids are included in the props file. You're going to have to parse the properties file and convert it yourself, the syntax of the file is documented in this IBM KnowledgeCenter topic. Given the complexity of this task, you may want to edit your question and add some detail on what you're trying to accomplish by converting to xml or json format file, so perhaps the community might better help you.

is it possible to define xml schema for a word document to be created by POI lib?

I have to generate word documents from my application against a entity which will contain some information about that entity, for this i am using POI. But while using POI i have to decide like where i have to create a paragraph, where i have make text bold\italic etc based on a configuration in entity object which i could easily handle in the code.
But is there any way so that i can just define all these style/alignments etc information in any XML/XSL or in any other type of config so i can get rid of styling in my java code ?
Regarding your title question, see Where can I find the XSDs of DOCX XML files?
Regarding your body question,
But is there any way so that i can just define all these
style/alignments etc information in any XML/XSL or in any other type
of config so i can get rid of styling in my java code ?
Yes, of course, and it would be a wise design decision to do so. Since DOCX is OOXML (within OPC) your XSLT will be able to generate OOXML character level formatting via w:rPr settings such as w:b, w:i, etc.
The challenge you'll be facing, however, is that you'll be forgoing the convenience provided by the POI API. You'll also have to reconstruct the OPC if you want to produce a proper DOCX file rather than just an importable OOXML file. For small projects, the learning curve required to wield OOXML directly is likely to be too steep to merit a direct-to-OOXML approach.

Java Unmarshalling an XML file using JAXB without knowing the tags

So I have about 950 XML files that I need to read, I have the Java classes for those XML files generated using an .xsd and a binding .xjb file.
My problem is that I need this code to be interchangeable, meaning it must be able to read different XML files with different tags, and still be able to return these values. I've tried using method.invoke to invoke all the get methods, but they aren't returned in a specific order, which doesn't help since I will need these values in an order to place it in a .csv file.
So if anyone can help me achieve this, it would be greatly appreciated.

Reading an XSD file to get all the elements and corresponding attributes in Java

My requirement is to read an XSD file and to get all its elements with corresponding attributes in java. I've been trying to convert my XSD file to a sample XML file as parsing an XML file to get all it's elements and attributes is easy. But so far I've been unable to find a good enough tool in java to programmatically convert my XSD to sample XML.
Is there any free and good java code available to convert an XSD to its sample XML?
Or else what is the way I can read all the elements and attributes that a sample XML would contain directly from the XSD file?
Thanks in advance!
Processing a raw XSD document as XML is quite tricky except in very simple cases.
Alternatives are:
(a) use an API for accessing a compiled schema (for example, there is such an API in Xerces)
(b) Saxon's schema processor can output an XML representation of the compiled schema, which is much easier to process than the raw XSD documents (for example, it combines everything into one document, and presents the relationship of elements to types in a uniform way).
The xsd format is perfectly valid XML, so you can parse an XML schema file with any xml parser.
Check this related post to get some code samples and ideas:
Java API to parse XSD schema file
Not sure if this solves the problem. I have similar requirement of accessing elements and attributes of xsd. Using Eclipse, the solution to create XML out of xsd is quite easy. I have a dynamic web project created and put my xsd in webcontent. Right click on XSD and there is option called 'Generate'. It shows 2 options to generate XML or JAXB classes. On clicking Generate XML, XML file is created from XSD. I hope you were looking for this solution.
There is a tool in java to convert my XSD to sample XML. You need to add jaxb-xjc (executable jar file) in the build path of your project.Once you are done with it just put the .xsd file in src, right click on it and find 'Generate' option, you can now find XML and Jaxb classes against 'Generate' option, Select Xml and get your XML file generated from .xsd.

Compare two almost similar xml files and then add tags missing in first xml file

I have to xml files say abc.xml & 123.xml which are almost similar, i mean has the same content, but the second one i.e, 123.xml has more content than the earlier one. I want to read both the files using Java, and compare whether the content present in abc.xml for each tag is same as that in 123.xml, and if 123.xml has some additional tags then add it into abc.xml same as remove tags from 123.xml if abc.xml doesnt have it.
I have two configuration files in xml old and new version. In new one tags could be added or removed and I want to generate xml file using content of old xml file and having xml schema of new xml file. Hope I am bit clear now.
That is a litle bit vague.
There are plenty off ways to do this. Espacialy when you think about how the merge should be done. Is one side always overriding the other one? This would mean also a remove of tags can happen when the newer file has less.
Here are two approaches how to handle this:
Use something existing like diff and patch as you can find in any version control system. You could use java to run the diff and patch application on the console.
In this case you would make a diff. If there is a difference use create patch to get a patch file describing the changes and then apply the patch.
If you wil do everything in java, i would not try to parse the file and work serialized on it. It will drive you crazy. I would use a kind of marshalling like JAX-B to convert the xml in a Pojo. Then you can compare the pojos and add or remove missing elements from the target pojo and then serialize it back to xml.
Hope this will get you a starting point for your further investigation ;)

Categories

Resources