Java DOM XML 解析::获取元素属性值

标签 java xml dom

如何从元素中提取属性值。我的xml节点是这样写的 < nodename attribute="value"> 我需要将其提取出来以将其与另一个字符串进行比较。

但是因为我没有调用 document.getElementsByTag,所以我不能使用 .getAttribute("att.").getNodeValue 来获取值。

相反,我有一个 NodeList 而 getAttribute() 没有 getNodeValue。

package dev;

import java.io.*;
import java.util.*;

import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;



public class Parser {


    static String def = "\"admin\",\"base\",\"Default\",\"simple\"";

    static String category = "";
    static String sku = "";
    static String has_options = "0";

    static String name = "";
    static String image = "";
    static String small_image = "";
    static String thumbnail = "";

    public static void toCSV() {
        try {
            BufferedWriter output = new BufferedWriter(new FileWriter("sim.csv", true));
            output.newLine();
            output.write(def);
            output.write(String.format(",\"%s\",\"%s\",\"%s\"", category, sku, has_options));
            output.write(String.format(",\"%s\",\"%s\",\"%s\",\"%s\"", name, image, small_image, thumbnail));
            output.flush();
            output.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        toCSV();
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();

            Document document = builder.parse(new File("input.asp.xml"));
            document.getDocumentElement().normalize();

            NodeList list = document.getElementsByTagName("izdelek");
            for(int i = 0; i < 1; i++) {
                NodeList child = list.item(i).getChildNodes();
                for(int j = 0; j < child.getLength(); j++) {
                    if(child.item(j).getNodeName().equals("kategorija")) {
                        category = child.item(j).getTextContent().trim();
                    } else if(child.item(j).getNodeName().equals("ean")) {
                        sku = child.item(j).getTextContent().trim();
                    } else if(child.item(j).getNodeName().equals("izdelekIme")) {
                        name = child.item(j).getTextContent().trim();
                    } else if(child.item(j).getNodeName().equals("slikaMala")) {
                        small_image = child.item(j).getTextContent().trim();
                        thumbnail   = child.item(j).getTextContent().trim();
                    } else if(child.item(j).getNodeName().equals("slikaVelika")) {
                        image = child.item(j).getTextContent().trim();
                    } else if(child.item(j).getNodeName().equals("dodatneLastnosti")) {
                        NodeList subs = child.item(j).getChildNodes();
                        // ^ need to parse these nodes they are written as <nodename attribute="value">
                        // i need to print out the value
                    } 
                }
                //toCSV();
            }

        } catch(Exception io) {
            io.printStackTrace();
        } 
    }


}

最佳答案

已解决:

XML 输入:

< nodename attribute="value" > Something </ nodename>

Java代码:

NodesList subs = child.item(j).getChildNodes();

System.out.println(subs.item(0).getTextContent()); // >> Something

Element element = (Element) document.adoptNode(subs.item(0));
System.out.println(element.getAttribute("attribute"));  // >> value

关于Java DOM XML 解析::获取元素属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9952220/

相关文章:

c# - 使用 c# 以编程方式读取 Openoffice Calc (.ods)?

xml - 如何使用 XPath 选择包含空元素的父元素?

php - 哪个更快 : read data from an XML file or from a MySQL table?

javascript - 显式地将 Any/Blob 转换为 ArrayBuffer 仍然会给我一个无法处理的 Blob

javascript - 如何使用 val() 与 js 和 jquery 获取输入值

java - Eclipse 格式化程序和双括号初始化

java - 在 Linux 环境中运行 java 应用程序时出现问题

java - 如何使用 Eclipse 调试某些 Java 类?

java - java main方法是一个线程吗?

javascript - 如何在svg元素上添加dragend事件