Results 1 to 8 of 8
Thread: xml parser
- 01-24-2012, 05:34 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
xml parser
I've never done this sort of parsing before and I'm having trouble getting it to work for my purposes. I'm using DOM parser to parse an xml file and then I want to retrieve all the values under "OperatingParameters." My code so far ... and the section of the xml file I'm interested in follow. I know the way I've done is wrong. Should I be using attributes instead...?
Java Code:import java.io.File; import org.w3c.dom.*; import javax.xml.parsers.*; public class XMLhandler { private File xmlFile; private Document doc; public XMLhandler(File file) { xmlFile = file; } public void parse() { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); doc = dBuilder.parse(xmlFile); } catch (Exception e) { e.printStackTrace(); } } public String[] getElements(String tagName) { String[] s = new String[100]; Element element = doc.getDocumentElement(); NodeList list = element.getElementsByTagName(tagName); for (int i = 0; i < element.getLength(); i++) { Element item = (Element) list.item(i); } return s; } }XML Code:<OperatingParameters Mode="Mass sweep" Focus1="-20" Focus2="-20" ElectronEnergy="70" FilamentEmission="2.0" AutoZero="Off" ScanMode="Sweep" Filament="1" PressureUnits="Torr" EnableElectronMultiplier="0" MultiplierVoltage="1110" />
- 01-24-2012, 05:41 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: xml parser
What happens when you use that code?
What do you see, and what do you want to see?
- 01-24-2012, 05:45 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Re: xml parser
Well, the NodeList I get back if, say, I were to print it contains only one element "OperatingParameters." what I want to be able to do is get a list of all the individual elements that follow OperatingParameters in the xml code I posted.
- 01-24-2012, 06:08 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: xml parser
As you guessed, though, there are no elements.
There are, however, attributes.
You could use getAttributes() on the Element and cycle through the Map that's returned?
- 01-24-2012, 06:11 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Re: xml parser
so this method should return all attributes in the element?
- 01-24-2012, 06:20 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: xml parser
Yes.
Here's the API for it (which I forgot to link to earlier).
- 01-24-2012, 06:37 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Re: xml parser
hmm...
If I insert this into my code:
Element element = doc.getDocumentElement();
NamedNodeMap map = element.getAttributes();
System.out.println(map.getLength());
map.getLength(); is zero
- 01-25-2012, 09:46 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
XML parser
By Lund01 in forum New To JavaReplies: 5Last Post: 03-21-2011, 04:50 PM -
JSP Parser????
By chathu03j in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-10-2008, 12:08 PM -
XML Parser
By samfuerte in forum XMLReplies: 1Last Post: 07-14-2007, 04:14 PM -
Parser
By Peter in forum Advanced JavaReplies: 2Last Post: 07-04-2007, 07:08 AM -
DKP Log Parser 1.4.1
By JavaBean in forum Java SoftwareReplies: 0Last Post: 06-25-2007, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks