小编And*_*lar的帖子

Java从xml递归读取节点只返回#text节点

我正在使用这种方法从 xml 文件中读取所有节点。但似乎我的递归不起作用,因为所有节点都是 #text 节点。我怎样才能跳过它并让它返回我的实际节点?

private void iterateNodes(Node node) {

    System.out.println("Node: " + node.getNodeName());

    NodeList nodeList = node.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentode = nodeList.item(0);

        System.out.println(currentode.getNodeName());

        if (currentode.getNodeType() == Node.ELEMENT_NODE) {

            Element element = (Element) currentode;
            iterateNodes(element);
        }
    }
}

public void run() throws ParserConfigurationException, SAXException, IOException {

    String path = "others.xml";

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document document = builder.parse(path);

    document.getDocumentElement().normalize();

    iterateNodes(document.getDocumentElement());

}
Run Code Online (Sandbox Code Playgroud)

java xml recursion

2
推荐指数
1
解决办法
1414
查看次数

如何将程序输出重定向到文本文件

我想将程序的输出重定向到文件。我怎样才能做到这一点?目前我的文件没有被创建,我只能将输出打印到我的控制台。

    int fd[2];
    int processId;
    int output;
    char filename[] = "output.txt";

    if((output = open(filename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1){
        fprintf(stderr, "Unable to create/open file '%s'\n", filename);
        return 1;
    }

    if(pipe(fd) == -1){
        fprintf(stderr, "Error creating pipe\n");
        return 2;
    }

    if((processId = fork()) == -1){
        fprintf(stderr, "Error forking\n");
        return 3;
    }   

    if(processId == 0){
        int newFD = dup(STDOUT_FILENO);
        char newFileDescriptor[2];
        sprintf(newFileDescriptor, "%d", newFD);
        dup2(fd[1], output);
        close(fd[0]);
        execl("./pipetest", "pipetest", newFileDescriptor, NULL);
    }else{
        close(fd[1]);
        char c[10];
        int r = read(fd[0], …
Run Code Online (Sandbox Code Playgroud)

c linux file

1
推荐指数
1
解决办法
3668
查看次数

标签 统计

c ×1

file ×1

java ×1

linux ×1

recursion ×1

xml ×1