1

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?

2
  • Have you tried jElement.getTextContent() instead of jElement.toString()? Commented Aug 12, 2013 at 12:40
  • No didn't tried because it is not going in if statement Commented Aug 12, 2013 at 12:59

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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")) {
                                ...

1 Comment

When I say 'NodeList list1 = doc.getDocumentElement().getChildNodes();' it returns null, so there is no node in this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.