gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
get element attibute value xml dom4j example
- User
get element attibute value xml dom4j example
- Assistant
Here is an example code to get an element attribute value using the dom4j API:
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Dom4jExample { public static void main(String[] args) { try { // Parse the XML file using SAXReader SAXReader reader = new SAXReader(); Document document = reader.read("example.xml"); // Get the root element of the document Element root = document.getRootElement(); // Get the value of an attribute of a specific element String attributeValue = root.element("child").attributeValue("attribute"); System.out.println("Attribute value: " + attributeValue); } catch (DocumentException e) { e.printStackTrace(); } } }In this example, we are reading an XML file using the SAXReader class and getting the root element of the document. Then, we are using the element() method to get a specific child element and the attributeValue() method to get the value of a specific attribute of that element. The value is then printed to the console.