This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML details

 
kademane guru
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am new to XML, i want to know how to read XML data in the java class?
without hard coding any things in java class. please help me out with some examples.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to XML Forum.
 
kademane guru
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Merrill Higginson,
thanks and please send me some good example's URL regarding XML, then it will be easy form me to understand.

Thanks & Regards
Kademane guru
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example code for using the DOM and SAX APIs to parse XML can be here.
 
kademane guru
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf Dittmer,
thanks, just i am getting some idea on XML.
i have some problem in my project.

let me explain my project first
in my project i am hardcoding some values(tags with values) in one XML file that file i am showing below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<Order>
<Manifest>
<Item>
<NAME>

<Name> Name</Name>
<Namelength>5</Namelength>
<Beginlength>10</Beginlength>
<Contains>10,20,30</Contains>
<Storequalification>nothing </Storequalification>
<Suffix>:</Suffix>

</NAME>

</Item>
</Manifest>
</Order>

i am doing my project using struts framework.

now i will tell my probelm.
1) I want read the tag names(i.e name,namelength,Beginlength,Contains) dynamically in struts action class, like if one array contains 10 values means we use for loop and we will print all the 10 values,like this i want to read only all the tag names which are in XML file.

2) I want read the tags like name,namelength,Beginlength,Contains etc.....
along with the values of the tags in the action class(struts action class),without hardcoding the tag names.

please provide the solution for this problem with some examples.


Thanks
Kademane guru
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use either DOM or SAX to do this. But if you want to avoid referring to element names, you will need to go by relative positions, which is an unstable thing to do. Why do you want to avoid using element names? If you don't want to hardcode them, read them from a property file instead.
 
kademane guru
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf Dittmer sir,
thanks for reply. but i dont know, how to read the XML Tags from the proeprty file. please guide me how to do this ?

Thanks
Kademane guru
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wouldn't read XML from a property file. What I suggested was a way to avoid hardcoding the names of the XML element in your XML parsing code. You would do that by reading their names from some property file. That file might look like this:



Then you don't have to refer to element "Name" in your code, but instead to the property "element.name". That way you can change the name of the element to (e.g.) "FirstName" later, and don't have to change the code; just the property file.
[ April 09, 2007: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic