如何解析Java中xml标记中引号的值?

时间:2022-11-27 17:00:40

Let us say I have an xml tag like:

我们假设我有一个xml标签,如:

<test val="val1 ">Test XML</test>

I want to parse the value "val1" whenever <test> tag occurs in the file so that I can save it to an array.

我想在文件中出现 标记时解析值“val1”,以便我可以将它保存到数组中。

1 个解决方案

#1


0  

You can load the XML from the file and select all elements with tag name test and extract attribute value from it.

您可以从文件加载XML并选择标记名为test的所有元素,并从中提取属性值。

Basically:

doc.getElementsByTagName("test") in order to select all "test" elements, and for each element found you can select its attribute named "val" with node.getAttributes().getNamedItem("val") and from this one, if not null just getTextContent() to take its text content.

doc.getElementsByTagName(“test”)以便选择所有“test”元素,并且对于找到的每个元素,您可以使用node.getAttributes()。getNamedItem(“val”)选择名为“val”的属性,并从此处,如果不为null,则只需getTextContent()来获取其文本内容。


A sample code could be something like:

示例代码可能类似于:

// temporary result list just to store the values found
ArrayList<String> result = new ArrayList<String>();

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(new ByteArrayInputStream("<document><test val=\"val1\">Test XML</test><test val=\"val2\">Test XML</test><test val=\"val3\">Test XML</test></document>".getBytes()));
// you could use something like: documentBuilder.parse(new FileInputStream("fileName.ext")); instead

// get test nodes and iterate over it checking test attribute
NodeList elements = doc.getElementsByTagName("test");
for (int i = 0; i < elements.getLength(); i++) {
    Node node = elements.item(i);
    Node attribute = node.getAttributes().getNamedItem("val");
    if (attribute != null) {
        result.add(attribute.getTextContent());
    }
}

// get result values in a string array (if collection is preferred use result instead)
String[] array = result.toArray(new String[] {});

// just print it in a readable format
System.out.println(Arrays.toString(array));

This will print:

这将打印:

[val1, val2, val3]

#1


0  

You can load the XML from the file and select all elements with tag name test and extract attribute value from it.

您可以从文件加载XML并选择标记名为test的所有元素,并从中提取属性值。

Basically:

doc.getElementsByTagName("test") in order to select all "test" elements, and for each element found you can select its attribute named "val" with node.getAttributes().getNamedItem("val") and from this one, if not null just getTextContent() to take its text content.

doc.getElementsByTagName(“test”)以便选择所有“test”元素,并且对于找到的每个元素,您可以使用node.getAttributes()。getNamedItem(“val”)选择名为“val”的属性,并从此处,如果不为null,则只需getTextContent()来获取其文本内容。


A sample code could be something like:

示例代码可能类似于:

// temporary result list just to store the values found
ArrayList<String> result = new ArrayList<String>();

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(new ByteArrayInputStream("<document><test val=\"val1\">Test XML</test><test val=\"val2\">Test XML</test><test val=\"val3\">Test XML</test></document>".getBytes()));
// you could use something like: documentBuilder.parse(new FileInputStream("fileName.ext")); instead

// get test nodes and iterate over it checking test attribute
NodeList elements = doc.getElementsByTagName("test");
for (int i = 0; i < elements.getLength(); i++) {
    Node node = elements.item(i);
    Node attribute = node.getAttributes().getNamedItem("val");
    if (attribute != null) {
        result.add(attribute.getTextContent());
    }
}

// get result values in a string array (if collection is preferred use result instead)
String[] array = result.toArray(new String[] {});

// just print it in a readable format
System.out.println(Arrays.toString(array));

This will print:

这将打印:

[val1, val2, val3]