• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing XML in String  RSS feed

 
don kris
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.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • 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.
 
Phil Hu
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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • 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
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate 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
 
Balaji Vankadaru
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.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!