Jump to content

Xml Newbie Question


kevinaworkman

Recommended Posts

Hello,I'm very new to XML Schemas, but I'm using them to send messages between a c/c++ backend (somebody else's job) and a java frontend (my job).The system works fine so far, but we're adding some functionality that I want to make sure I have my head wrapped around. I just wanted to make sure that I'm fully understanding the process here. The backend sends me an XML file that looks something like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><programMessage xmlns="http://www.thisIsJustASample.org/program" xmlns: xsd="http://www.w3.org/2001/XMLSchema"><monitoredResource>   <theTagInQuestion>		<childOfTagInQuestion>string data</childOfTagInQuestion>		<childOfTagInQuestion>string data</childOfTagInQuestion>		<childOfTagInQuestion>string data</childOfTagInQuestion>		<childOfTagInQuestion>string data</childOfTagInQuestion>		<childOfTagInQuestion>string data</childOfTagInQuestion>		<childOfTagInQuestion>string data</childOfTagInQuestion>		<childOfTagInQuestion>string data</childOfTagInQuestion>		<childOfTagInQuestion>string data</childOfTagInQuestion>   </theTagInQuestion></monitoredResource>

So basically, a monitoredResource contains a theTagInQuestion, which contains 8 childOfTagInQuestion. The ultimate goal is to convert the strings contained by childOfTagInQuestion into a list in java using JAXB.My question is, does this schema look correct so far? This should contain a monitoredResource, which contains a theTagInQuestion, which is a list of 8 childOfTagInQuestion, which, finally, are strings. Am I doing something wrong?The schema looks like this:

<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:pg="http://www.thisIsJustASample.org/program" xmlns="http://www.w3.org/2001/XMLSchema"><xsd:element name="monitoredResource">   <xsd:complexType>	  <xsd:sequence>		 <xsd:element ref="pg:theTagInQuestion"/>	  </xsd:sequence>   </xsd:complexType></xsd:element><xsd:element name="theTagInQuestion"/><xsd:simpleType>	  <xsd:list itemType="pg:childOfTagInQuestion"/></xsd:simpleType></xsd:element><xsd:simpleType name="childOfTagInQuestion">   <xsd:restriction  base="xsd:string" xsd:default="DEFAULT">   </xsd:restriction></xsd:simpleType></xsd:schema>

Does this look like what I described? If not, what should I change? Also, ultimately childOfTagInQuestion might actually be a list of strings, so theTagInQuestion will actually be a list of lists. How would I go about doing that?I appreciate anybody's help on this. I'm very new to using XML (I love java, but these markup languages just confuse me), so any assistance is greatly appreciated!

Link to comment
Share on other sites

xsd:simpleType refers to a type that contains just text. So, what you're saying with

<xsd:element name="theTagInQuestion"/><xsd:simpleType>	  <xsd:list itemType="pg:childOfTagInQuestion"/></xsd:simpleType></xsd:element>

Is that theTagInQuestion has a list (separated by a whitespace I believe) each of which is of type "pg:childOfTagInQuestion". For example:

<theTagInQuestion>value anotherValue aThirdValue</theTagInQuestion>

What you define earlier about monitoredResource is the right way to do it - define an xsd:complexType (which refers to a type that contains elements, possibly with text) which defines pg:childOfTagInQuestion as an allowed element. To define that it can occur multiple times, use the maxOccurs attribute on xsd:element, like so:

<xsd:element name="theTagInQuestion">   <xsd:complexType>	  <xsd:sequence>		 <xsd:element ref="pg:childOfTagInQuestion" maxOccurs="unlimited" />	  </xsd:sequence>   </xsd:complexType></xsd:element>

And make sure you're defining pg:childOfTagInQuestion as an element further on:

<xsd:element name="childOfTagInQuestion" type="xsd:string" />

I'd highly reccomend you get a specialized schema editor, or at least one that can validate a schema and an XML file against it. It it, make sure that whatever schema you create is valid and validates against a few samples of actual messages you're going to be processing.P.S. You do realize that the presence of a schema alone is not going to convert the element into a ArrayList (or whatever JAVA construct you want to use), right? It's just for validation i.e. making sure the C(++) backed has given you a message you can convert without an error.

Link to comment
Share on other sites

Thanks a ton, you've answered my question completely!

P.S. You do realize that the presence of a schema alone is not going to convert the element into a ArrayList (or whatever JAVA construct you want to use), right? It's just for validation i.e. making sure the C(++) backed has given you a message you can convert without an error.
Aw man, you mean I actually have to DO stuff with these message?! :) I might be a newbie, but I'm not that dumb. I'm using JAXB to unmarshall the data contained by the XML messages. Creating this schema was just the first step. The java stuff I can handle, but I always have a hard time wrapping my head around exactly what to do with XML.Thanks for all your help, I appreciate it!
Link to comment
Share on other sites

Aw man, you mean I actually have to DO stuff with these message?! :) I might be a newbie, but I'm not that dumb.
I'm sorry that I ever doubted you :) . It's just that the majority of people with Schema questions don't realize what Schema is used for. The other part do, but want to declare the one thing Schema (or DTD, or RNG) can't declare - making the contents of one node be dependant on the contents of another node.
Link to comment
Share on other sites

Understood. Everything works great now, I'm getting the strings into my java program exactly how I wanted to.However, there is one problem that I knew would pop up. Since the strings (within childOfTagInQuestion) themselves are actually a series of data separated by spaces, like this: "stringOne stringTwo stringThree", I'm gathering that XML considers them to be a list of strings, with each element separated by white space, as opposed to one longer string containing the data and spaces? So, I'm actually only getting the first part of each string that I want, and losing the data after the first space?So, I suppose that just means I have to declare childOfTagInQuestion to be a list of strings, thereby making theTagInQuestion a list of lists. Is that possible? I looked online but I can't see anywhere with an example of how to do a 2-dimensional array like that.. but probably just because it's something so simple that people don't bother creating tutorials for it!Thanks again!

Link to comment
Share on other sites

Understood. Everything works great now, I'm getting the strings into my java program exactly how I wanted to.However, there is one problem that I knew would pop up. Since the strings (within childOfTagInQuestion) themselves are actually a series of data separated by spaces, like this: "stringOne stringTwo stringThree", I'm gathering that XML considers them to be a list of strings, with each element separated by white space, as opposed to one longer string containing the data and spaces? So, I'm actually only getting the first part of each string that I want, and losing the data after the first space?So, I suppose that just means I have to declare childOfTagInQuestion to be a list of strings, thereby making theTagInQuestion a list of lists. Is that possible? I looked online but I can't see anywhere with an example of how to do a 2-dimensional array like that.. but probably just because it's something so simple that people don't bother creating tutorials for it!Thanks again!
You did exactly that in your initial example by declaring a simple type as a list. So following that:
<xsd:simpleType name="childOfTagInQuestion">	  <xsd:list itemType="xsd:string"/></xsd:simpleType>

then simply declare childOfTagInQuestion as this type:

<xsd:element name="childOfTagInQuestion" type="pg:childOfTagInQuestion" />

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...