aspose file tools*
The moose likes XML and Related Technologies and the fly likes XML block extraction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML block extraction" Watch "XML block extraction" New topic
Author

XML block extraction

Matthew Tilic
Ranch Hand

Joined: Apr 07, 2011
Posts: 41

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Apr 07, 2011
Posts: 41

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?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Try http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html and http://www.zvon.org/xxl/XPathTutorial/General/examples.html
Matthew Tilic
Ranch Hand

Joined: Apr 07, 2011
Posts: 41

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

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Apr 07, 2011
Posts: 41

How would I best parse that? as I could do that. Not an XML pro as you guys can tell
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Apr 07, 2011
Posts: 41

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
Bartender

Joined: Oct 14, 2005
Posts: 18109
    
    8

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

Joined: Apr 07, 2011
Posts: 41

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
Bartender

Joined: Oct 14, 2005
Posts: 18109
    
    8

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

Joined: Apr 07, 2011
Posts: 41

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
Bartender

Joined: Oct 14, 2005
Posts: 18109
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML block extraction
 
Similar Threads
[Hibernate] One-to-many batch updating?
Problem with Result Set pointer.
How to update the xml tag attributes for the given tag
Only fun forwards for fun ...
xml parser design question