Parsing pblm.

Discussions

XML & Web services: Parsing pblm.

  1. Parsing pblm. (2 messages)

    I am using DOM parser to parse the following XML

    <CommandMaps>
      <CommandMap>
        <Name method="setName">Login.do</Name>
        <Description method="setDescription">Desc</Description>
        <ActionFormHelper>AFH</ActionFormHelper>
        <NextCommand method="setNextCommand"></NextCommand>
        <ServiceName method="setServiceName">SN</ServiceName>
        <ValidationFlag>N</ValidationFlag>
        <ValidationObjectName></ValidationObjectName>
        <SuccessWorkFlowName>SWFName</SuccessWorkFlowName>
        <FailureWorkFlowName>FWFName</FailureWorkFlowName>
      </CommandMap>
      ...
      ...
    </CommandMaps>

    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.io.File;
    import java.io.IOException;
    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)
        throws
          ParserConfigurationException,
          IOException,
          SAXException,
          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.");
          System.exit(1);
        } catch (java.io.IOException ie) {
          System.out.println("Cannot read input file.");
          System.exit(1);
        } catch (org.xml.sax.SAXException se) {
          System.out.println("Problem parsing the file.");
          System.exit(1);
        } catch (java.lang.IllegalArgumentException ae) {
          System.out.println("Please specify an XML source.");
          System.exit(1);
        }
        // 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();
        System.out.println(
          "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) {

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

              //CommandMapBean cmbean =
              // new CommandMapBean(commandName, afhName, serviceName, swfName, fwfName);
              //System.out.println(cmbean.toString());
            }
          }
          indx++;
        }
      }
    }

    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());


    Fredrik...