• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Parsing XML in String

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

I am a newbie to Java world and struggling to parse a XML string that has attributes in them.

Can anyone help me how to parse this and get all the data? My XML string can change size depending on the NumOfParams.

Below is the XML:

<PictureManager> <FuncName>DisplayImage</FuncName> <NumOfParams>2</NumOfParams> <Parameters> <Param type="integer">10</Param> <Param type="String">C://Me.jpg</Param>; </Parameters> </PictureManager>

I need to be able to get "integer" and "String" attributes also from the XML. The XML string could grow or shrink based on the <NumOfParams>

It will help a lot if you can post some code that actually creates a XML string with the above mentioned tags.

Thanks in advance.
 
Marshal
Posts: 64643
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I am afraid we don't provide ready made code. I presume you have found XML tutorials, etc. I know precious little XML myself; I shall copy this question to another forum in the hope of your getting more responses there.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!

I dont know whether I got you right but here is my idea of a function to read out all attributes and concat them



This would return "type integer" for the following function call:




The function local:createString is just a dummy function which could help you for the completion of your task ;)
I hope this helps you ;)
Cheers, Phil
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil, I don't know what programming language that is, but it is certainly not Java.
 
Phil Hu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Phil, I don't know what programming language that is, but it is certainly not Java.



No it isnt..
Because of the fact that I found the topic in the "XML"-part of the forum, I provides some XQuery code..
I wasnt sure whether he wants to use Java or XQuery..

Cheers, Phil
 
Bartender
Posts: 10759
68
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

don kris wrote:Can anyone help me how to parse this and get all the data? My XML string can change size depending on the NumOfParams.


There are two basic parsing frameworks for XML: SAX and DOM, both of which have Java libraries you can use. The tutorial for SAX (which is the simpler of the two) is here.

The only other thing I can advise: DON'T use regular expressions. They may seem like a great idea, but they are NOT suited for this sort of work.

Winston
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can use JaxB to parse your xml into java objects.

this is one of the easy way you can do else you can your sax or dom to parse your xml file.
 
Rancher
Posts: 43009
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

don kris wrote:It will help a lot if you can post some code that actually creates a XML string with the above mentioned tags.


I'm confused: it seems you already have an example string - what else are you looking for, and why?

Winston Gutkowski wrote:There are two basic parsing frameworks for XML: SAX and DOM, both of which have Java libraries you can use.


Well, the JRE also comes with XPath and StAX and JAXB, so options abound. If this was my problem (assuming the problem has been stated in its entirety), I might actually choose XPath.

Balaji Vankadaru wrote:Well you can use JaxB to parse your xml into java objects.


This of course requires the classes for those objects to exist. So there is some start-up overhead to using JAXB.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!