开发者

how to get inner xml tag values through java?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜