Question

I'm trying to use windows's task scheduler xml file. For anyone who doesn't know, xml file looks like this:

  <?xml version="1.0" encoding="UTF-16"?>
<Tasks>
    <!-- \job1 -->
    <Task version="1.2"
        xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
        <RegistrationInfo>
            <Date>2013-08-03T13:07:36.8791439</Date>
            <Author>pc\laptop</Author>
        </RegistrationInfo>
    ... 
        <!-- \job2 -->
         <Task version="1.2"
         ......

I need to assign "job1" value to an array. To do this I did this java code:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringComments(false);
NodeList jobList = el.getElementsByTagName("Tasks");
    NodeList ndList = el.getElementsByTagName("Task");

    for (int i = 0; i < ndList.getLength(); i++) {
        Node childNode2 = jobList.item(i);
        Node childNode = ndList.item(i);
        if (childNode2.getNodeType() == Node.COMMENT_NODE) {

            //Element jElement = (Element) childNode2;
            //jElement.toString();
            System.out.println(childNode2.getNodeType());
              }
             if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) childNode;
        System.out.println("Date : " + getTagValue("Date", eElement));
        //some other codes....
        }

But I cannot reach that comment area. Second thing is how can I walk in that comment areas. How do I write a for loop for example?

No correct solution

OTHER TIPS

You are missing the Comments because you are using getElementsByTagName() which return a NodeList each of Element type.

Use getChildNodes() on Document instead, which return a node list of any sub interface of Node, including Comment

This should work

    NodeList list1 = doc.getDocumentElement().getChildNodes();
    for (int i = 0; i < list1.getLength(); i++) {
        Node n1 = list1.item(i);
        if (n1.getNodeType() == Node.COMMENT_NODE) {
            String comment = n1.getNodeValue();
        } else if (n1.getNodeType() == Node.ELEMENT_NODE) {
            // Task
            NodeList list2 = n1.getChildNodes();
            for (int j = 0; j < list2.getLength(); j++) {
                Node n2 = list2.item(j);
                if (n2.getNodeType() == Node.ELEMENT_NODE) {
                    // RegistrationInfo 
                    NodeList list3 = n2.getChildNodes();
                    for (int k = 0; k < list3.getLength(); k++) {
                        Node n3 = list3.item(k);
                        if (n3.getNodeType() == Node.ELEMENT_NODE) {
                            if (n2.getNodeName().equals("Date")) {
                                ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top