开发者

how to get inner xml tag values through java?

开发者 https://www.devze.com 2023-03-09 21:30 出处:网络
how i can find the \"abc\" from this tag through JAVA code and SAX parser. <first name=\"abc\">My Text<first>

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

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

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

NodeList firstNameList = firstPersonElement.getElementsBy开发者_如何学PythonTagName("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>

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


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

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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消