parsing an xml schema to read/list all elements - java

So, I wish to parse an xml schema and list all the elements along with their annotation and type. I looked at some java possibilities - the closest was XSOM. It seems like driving a truck trailer to get some milk from the neighborhood store.
I looked at JAXB, but there's no parse and list all elements against schemata.
I don't want to validate- only want to list the elements/type/annotation.
Groovy's xmlsurper is a decent parser, but can't parse XSD. Anything you know in Java,Groovy (or python)?
thank you for your time.

The SAX parser is very simple.

Related

Sort XML alphabetically while mantaining XSD schema validity

I have an XML file that is valid against an XSD schema. I would like to sort it alphabetically by applying the following criteria (in order of priority):
- by element name
- by attribute names
- by attribute values
Furthermore, I would like the sorted XML file to be valid against the same XSD schema. Is there an existing XML sorting algorithm that would comply to my requirements? If not, what would be the best technical approach to write such an algorithm (eg: use XSLT)?
Based on my previous analysis, I tried to figure out a correct approach for the "sequence", "choice" and "all" elements in the XSD but failed to succeed. I am using dom4j 1.6.1 for my current processing tasks. Looking forward to your suggestions.

Store XML data in DOM parser [duplicate]

I am new working in Java and XML DOM parser. I had a requirement like read the xml data and store it inform of column and rows type.
Example:sample.xml file
<staff>
<firstname>Swetha</firstname>
<lastname>EUnis</lastname>
<nickname>Swetha</nickname>
<salary>10000</salary>
</staff>
<staff>
<firstname>John</firstname>
<lastname>MAdiv</lastname>
<nickname>Jo</nickname>
<salary>200000</salary>
</staff>
i need to read this XML file and store it in the above format:
firstName,lastName,nickName,Salary
swetha,Eunis,swetha,10000
john,MAdiv,Jo,200000
Java Code:
NodeList nl= doc.getElementsByTagName("*");
for(int i=0;i< nl.getLength();i++)
{
Element section = (Element) nl.item(i);
Node title = section.getFirstChild();
while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
{
title = title.getNextSibling();
if (title != null)
{
String first=title.getFirstChild().getNodeValue().trim();
if(first!=null)
{
title = title.getNextSibling();
}
System.out.print(first + ",");
} }
System.out.println("");
}//for
I did the above code, but i am not able to find the way to get the data in the above column and row format. Can any one please please kindly help me in solving my issue, i am looking into it from past many days
Since this looks like homework, I'm going to give you some hints:
The chances are that your lecturer has given you some lecture notes and/or examples on processing an XML DOM. Read them all again.
The getElementsByTagName method takes an element name as a parameter. "*" is not a valid element name, so the call won't return anything.
Your code needs to mirror the structure of the XML. The XML structure in this case consists of N staff elements, each of which contains elements named firstname, lastname, nickname and salary.
It is also possible that your lecturer expects you to use something like XSLT or an XML binding mechanism to simplify this. (Or maybe this was intended to be XMI rather than XML ... in which there are other ways to handle this ...)
I kept getElementsByTagName method parameter "*" because to read the data dynamically.
Well, it doesn't work!! The DOM getElementsByTagName method does NOT accept a pattern of any kind.
If you want to make your code generic, you can't use getElementsByTagName. You will need to walk the tree from the top, starting with the DOM's root node.
Can you please provide me with sample data.
No. Your lecturer would not approve of me giving you code to copy from. However, I will point out that there are lots of XML DOM tutorials on the web which should help you figure out what you need to do. The best thing is for you to do the work yourself. You will learn more that way ... and that is the whole point of your homework!
1. The DOM Parser will parse the entire XML file to create the DOM object.
2. You will always need to be aware of the the type of output and the structure of xml returned when a request is fired on a web-service.
3. And its Not the XML structure of a reply which is returned from the Webservice that will be dynamic, but the child elements values and attributes can be Dynamic.
4. You will need to handle this dynamic behavior with try/catch block...
For further details on DOM PARSER, see this site...
http://tutorials.jenkov.com/java-xml/dom.html

How to compare document objects in java with .xsd files?

I am trying to compare Document objects to understand if they are well formed or not. So to do that, I made a research about it and heard that xsd files are used to make this comparison. Can you please give me some basci examples to compare document with using xsd objcets ?
For example what do I have to write into xsd file and how I can compare it with a Document object ?
Thank you all
You don't need an XSD schema to determine if a document is well-formed. You only need it to determine if the document is valid against the schema.
I'm not sure what you mean by "comparing XML documents". What are you comparing them with?

Parsing XML like this

<item>
<RelatedPersons>
<RelatedPerson>
<Name>xy</Name>
<Title>asd</Title>
<Address>abc</Address>
</RelatedPerson>
<RelatedPerson>
<Name>xy</Name>
<Title>asd</Title>
<Address>abc</Address>
</RelatedPerson>
</RelatedPersons>
</item>
I d like to parse this data with a SAXParser. How can i do this?
I know the tutorials about SAX, and i can parsing any normal RSS, but i can't parsing this datas only.
Define your Problem: What you can probably do is create a Value Object(POJO) called Person which has the properties: name, title and address. You aim of parsing this XML would then be to create an ArrayList<Person> object. Defining a definite data structure helps you build logic around it.
Choose a Parser : You can then use a SAX Parser or an XML Pull Parser to browse through the tags: see this lin for a tutorial on DOM, SAX and XML Pull Parser in Android.
Data Population Logic: Then while Parsing, whenever you encounter a <RelatedPersons> tag, instantiate a new Person object. When you encounter the respective Properties tag, read the value and populate it in this object. When you encounter a closing </RelatedPersons> dump this Person Object in the ArrayList. Depending on the Parser you use, you will have to use appropriate methods to browse to the child node/nested nodes.(Refer the link for details)
By the time you are done parsing the last item node you will have all the values in your ArrayList.
Note that this is more of a theoretical answer; I hope it helps.

How to sort xml files using SAX

I have 6 XML files containing the following tag
the first XML file is
<root>
<firstName> Smith</firstName>
<lastname>Joe</lastname>
<Age>60</age>
</root>
the second is
<root>
<firstName> John</firstName>
<lastname>Andrew</lastname>
<Age>55</age>
</root>
and so on
the required is to print the firstname,lastname,age and I have done that in agood way.However, I need also to
print ages sorted by age
so first should be 55 then 60. I could not do that by sax it was really
IF you use sax parser you should use some intermediate structure and sort it in it (like one of the Collections). Sax parser is event based so you can't sort it really using it.
The only possible reason for using SAX is because you don't want to allocate memory to store the whole document. If you're sorting, then SAX gives you no benefits - you're using a very low-level interface to no purpose. If you want to sort the data then by far the best solution is to use a high-level XML processing language such as XSLT or XQuery.

Categories