Parsing pblm.


XML & Web services: Parsing pblm.

  1. Parsing pblm. (2 messages)

    I am using DOM parser to parse the following XML

        <Name method="setName"></Name>
        <Description method="setDescription">Desc</Description>
        <NextCommand method="setNextCommand"></NextCommand>
        <ServiceName method="setServiceName">SN</ServiceName>

    I would like to create a collection of beans...
    The SOP statment 1 gives the element name but the SOP statement 2 gives NULL.

    Can any one help?

    import java.util.HashMap;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;

    public class XMLReader {

      private static HashMap hashMap;
      private String elementName;
      private StringBuffer elementValue;

      public static void readXML(String fileNm)
          IllegalArgumentException {

        Document doc = null;
        try {

          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();

          doc = db.parse(new File("fw_command_maps.xml"));

        } catch (javax.xml.parsers.ParserConfigurationException pce) {
          System.out.println("The parser was not configured correctly.");
        } catch ( ie) {
          System.out.println("Cannot read input file.");
        } catch (org.xml.sax.SAXException se) {
          System.out.println("Problem parsing the file.");
        } catch (java.lang.IllegalArgumentException ae) {
          System.out.println("Please specify an XML source.");
        // Getting the document element
        Element root = doc.getDocumentElement();
        System.out.println("The root element is " + root.getNodeName());

        // Getting the child nodes
        NodeList commandMapList = root.getChildNodes();
          "There are " + commandMapList.getLength() + " nodes in this document.");

        Node commandMapNode;
        int indx = 0;
        String commandName = "";
        String afhName = "";
        String serviceName = "";
        String swfName = "";
        String fwfName = "";

        while (indx < commandMapList.getLength()) {
          commandMapNode = commandMapList.item(indx);

          for (int i = 0; i < commandMapNode.getChildNodes().getLength(); i++) {

            if (commandMapNode.getChildNodes().item(i).getNodeType() == 1) {

                "Node Name::." + commandMapNode.getChildNodes().item(i).getNodeName());
                "Node Value::." + commandMapNode.getChildNodes().item(i).getNodeValue());

              //CommandMapBean cmbean =
              // new CommandMapBean(commandName, afhName, serviceName, swfName, fwfName);

    Threaded Messages (2)

  2. Parsing pblm.[ Go to top ]

    Because the nodeValue for element (type=1) is always null.
  3. Parsing pblm.[ Go to top ]

    This is because in DOM, the value (text) of the node is in a sub-node. If you apply getFirstChild() on the node you will get the child text node, and then you can do getNodeValue() on the text node. That would give

    System.out.println("Node Value::." + commandMapNode.getChildNodes().item(i).getFirstChild().getNodeValue());