Results 1 to 8 of 8

Thread: xml parser

  1. #1
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default 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"
       />

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: xml parser

    What happens when you use that code?
    What do you see, and what do you want to see?

  3. #3
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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?

  5. #5
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default Re: xml parser

    so this method should return all attributes in the element?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: xml parser

    Yes.
    Here's the API for it (which I forgot to link to earlier).

  7. #7
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default 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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: xml parser

    Because your document element (which is the root of your XML) probably doesn't have any attributes?
    I thought you were going to do this on the elements in your NodeList?

Similar Threads

  1. XML parser
    By Lund01 in forum New To Java
    Replies: 5
    Last Post: 03-21-2011, 05:50 PM
  2. JSP Parser????
    By chathu03j in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-10-2008, 01:08 PM
  3. XML Parser
    By samfuerte in forum XML
    Replies: 1
    Last Post: 07-14-2007, 05:14 PM
  4. Parser
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 08:08 AM
  5. DKP Log Parser 1.4.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 06-25-2007, 09:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •