• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML block extraction

 
Matthew Tilic
Ranch Hand
Posts: 41
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is my XML:



Now I want to take blocks of that code as in take teacher out and process, then the students and process like below:









Now for my parser I can just search for the tags, however, if I search for name then I would get many results. Need to do this as fast as possible for my Java server. What is the best way to extract this Tags and all? Reason is I don't want just the results as I may want to do further reading

THanks in advance
 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a case for XPath, which makes it easy to pick out bits of XML data. It depends a bit on what further "processing" you intend to do.
 
Matthew Tilic
Ranch Hand
Posts: 41
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will look at the tag name and have a switch to see if the processing needs to be taken any further.

I will look up XPath. Any good tutorials you know of?
 
Matthew Tilic
Ranch Hand
Posts: 41
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think I want to use XPath as I realised that I will have to create a query and this would not work for me.

This is more what my xml would look like


I want the see the student block and then read the node title and extract the node student then read the information in it and each node value will be a case which I can switch to and run the function that belongs to it.

Does this make sense?
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The College document below may be a bit easier to handle (and more efficient.) XML Parsers read attributes faster than elements.

 
Matthew Tilic
Ranch Hand
Posts: 41
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I best parse that? as I could do that. Not an XML pro as you guys can tell
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no "best" way to process anything. There are many factors to consider. The primary one is your knowledge, skills and abilities. You could process it with Perl, or C++, or Java.

In Java for example, there are many programming APIs to help with processing XML-based documents. If you are just starting out, it is "best" to learn the Simple API for XML(SAX) first.
 
Matthew Tilic
Ranch Hand
Posts: 41
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have pretty much followed this: http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/


and the only difference that I want to do (as I think you already understand) is that I would like to see in this xml is to read the nodes in one by one and depending on the tag preform an action instead of in the example actually searching for the node (in the example shown they search for person)
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you spend some time learning about XML before you proceed. Right now it appears you have made up your own terminology for things, which is going to make it hard for you to understand documentation and tutorials for a start.

For example I think what you are calling a "block" is what XML calls an "element". What you mean by "node title", I can't tell. At any rate the terminology is there for a reason, namely so we're all using the same words for things. Having to struggle through other people's words is just painful. I suggest Processing XML with Java as a good starting point, and the second chapter describes terminology.
 
Matthew Tilic
Ranch Hand
Posts: 41
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes sorry I am writing poorly,
I will re-write the sentence correctly:


I have pretty much followed this: http://www.developerfusion.com/code/2064/a-simple-...y-to-read-an-xml-file-in-java/

and the only difference that I want to do (as I think you already understand) is that I would like to read each of the child elements tags and then depending on the tag data perform a task. I would like to work my way down the XML file then searching for specific tags instead of in the example actually searchs for the element (in the example shown they search for person).

I hope this reads better. I will read the document, however, I need to finish this project quickly as have been extremely slack over Christmas. Thanks
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So you want to use DOM processing to go through your document. But I don't understand your problem. You say you don't want to search for particular element names, you just want to read all of the elements and do some processing for each element based on its name, or something along that line. So what's your problem with that?

(By the way DOM doesn't give you access to the "tags". You only see start and end tags in the text version of the document. DOM restructures the document into a tree consisting of nodes.)
 
Matthew Tilic
Ranch Hand
Posts: 41
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have got my issue bang on the nail correct.

My question is that how do I convert that search to parse the elements and retrieve the information?
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're navigating through a tree. You start with the document element and process its child nodes. If necessary, you process the children of those child nodes, and so on. I'm sure the link I posted has examples of how to do DOM navigation, and I'm sure there are more examples on the web than just that one example you looked at.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic