我有一個XML文件,這種結構的XML文件的屬性:得到使用Java
<?xml version="1.0">
<person>
<element att1="value1" att2="value2">Anonymous</element>
</person>
如何使用您想wathever我提取屬性的名稱和值。
我試過JDOM,但我仍然無法找到從元素中獲取屬性的方法。
Element root = doc.getRootElement();
List allChildren = root.getChildren();
Iterator i = listEtudiants.iterator();
while(i.hasNext())
{
Element current = (Element)i.next();
System.out.println(current.getChild("elementName").getText());
// this let me get just the value inside > anf </
// so, if it's can be done by completing this code
// it will be something like current.getSomething()
}
感謝
編輯:我仍然有這個文件有問題。我無法達到foo屬性和它的價值moo。
<?xml version="1.0" encoding="UTF-8"?>
<person>
<student att1="v1" att2="v2">
<name>Michel</name>
<prenames>
<prename>smith</prename>
<prename>jack</prename>
</prenames>
</student>
<student classe="P1">
<name foo="moo">superstar</name>
</student>
</person>
好的,現在假設你不知道元素裏面有什麼! –
你是什麼意思?你不知道屬性的名字? –
getAttributes()可能是你正在尋找的。 http://www.jdom.org/docs/apidocs/org/jdom/Element.html#getAttributes() – NeilMonday