如何通过java获取内部xml标记值?

时间:2023-01-14 08:49:26

how i can find the "abc" from this tag through JAVA code and SAX parser.

如何通过JAVA代码和SAX解析器从这个标记找到“abc”。

<first name="abc">My Text<first>

for example i am using the java code given below to find "My Text" from the above tag.

例如,我正在使用下面给出的java代码从上面的标记中查找“我的文本”。

NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
Element firstNameElement = (Element)firstNameList.item(0);
String type = firstPersonElement.getTextContent();

NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : " + 
       ((Node)textFNList.item(0)).getNodeValue().trim());

but i don't know how to find "abc" from <first name="abc">My Text<first>

但是我不知道如何从< name="abc">中找到"abc"我的文本

I searched by myself but i didn't find my exact solution.

我自己搜索了一下,但没有找到确切的答案。

2 个解决方案

#1


4  

name="abc" is an attribute of the first element.

name="abc"是第一个元素的属性。

String name = firstNameElement.getAttribute("name"); // "abc"

#2


1  

You are using DOM (Document Object Model), and not SAX (Simple API for XML).

您使用的是DOM (Document Object Model),而不是SAX(用于XML的简单API)。

Once you have an element, you can access the 'element.getAttribute("name")' to get the value you want.

一旦您有了一个元素,您就可以访问“element.getAttribute(“name”)”来获得您想要的值。

#1


4  

name="abc" is an attribute of the first element.

name="abc"是第一个元素的属性。

String name = firstNameElement.getAttribute("name"); // "abc"

#2


1  

You are using DOM (Document Object Model), and not SAX (Simple API for XML).

您使用的是DOM (Document Object Model),而不是SAX(用于XML的简单API)。

Once you have an element, you can access the 'element.getAttribute("name")' to get the value you want.

一旦您有了一个元素,您就可以访问“element.getAttribute(“name”)”来获得您想要的值。