Java Tutorial/XML/Attribute
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
- 1 Accessing attributes of an element
- 2 Adding and Removing an Attribute in a DOM Element
- 3 Determining If an Attribute Was Supplied in a DOM Element
- 4 Extracting attribute values from XML elements
- 5 Getting and Setting an Attribute in a DOM Element
- 6 Listing All the Attributes of a DOM Element
- 7 Remove all attributes by first making a copy of the attribute names and then using the list to remove the attributes:
- 8 Removing All the Attributes in a DOM Element
Accessing attributes of an element
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
public class Main {
public static void main(String[] argv) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder loader = factory.newDocumentBuilder();
Document document = loader.parse("sample.xml");
Element purchaseOrder = document.getDocumentElement();
Attr orderDate = purchaseOrder.getAttributeNode("date");
System.out.println(orderDate.getValue());
NamedNodeMap attrs = purchaseOrder.getAttributes();
int attrsCount = attrs.getLength();
for (int i = 0; i < attrsCount; i++) {
Attr item = (Attr) attrs.item(i);
System.out.println(""" + item.getName() + "" = "" + item.getValue() + """);
}
}
}
Adding and Removing an Attribute in a DOM Element
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Main {
public static void main(String[] argv) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
element.setAttribute("newAttrName", "attrValue");
element.setAttribute("newAttrName", "<>&\""");
element.removeAttribute("value");
boolean has = element.hasAttribute("value"); // true
String attrValue = element.getAttribute("value"); // mydefault
}
}
Determining If an Attribute Was Supplied in a DOM Element
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Main {
public static void main(String[] argv) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
Attr attr = (Attr) element.getAttributeNode("attrName");
boolean wasSpecified = attr != null && attr.getSpecified();
}
}
Extracting attribute values from XML elements
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Main {
public static void main(String[] argv) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
SaxHandler handler = new SaxHandler();
parser.parse("sample.xml", handler);
}
}
class SaxHandler extends DefaultHandler {
public void startElement(String uri, String localName, String qName, Attributes attrs)
throws SAXException {
if (qName.equals("order")) {
String date = attrs.getValue("date");
String number = attrs.getValue("number");
System.out.println("Order #" + number + " date is "" + date + """);
}
}
}
Getting and Setting an Attribute in a DOM Element
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Main {
public static void main(String[] argv) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
boolean has = element.hasAttribute("value");
String attrValue = element.getAttribute("value");
element.setAttribute("value", "newValue1");
element = doc.getElementById("key2");
has = element.hasAttribute("value");
attrValue = element.getAttribute("value");
element.setAttribute("value", "a<\""&>z");
}
}
Listing All the Attributes of a DOM Element
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
public class Main {
public static void main(String[] argv) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
NamedNodeMap attrs = element.getAttributes();
int numAttrs = attrs.getLength();
for (int i = 0; i < numAttrs; i++) {
Attr attr = (Attr) attrs.item(i);
String attrName = attr.getNodeName();
String attrValue = attr.getNodeValue();
}
}
}
Remove all attributes by first making a copy of the attribute names and then using the list to remove the attributes:
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
public class Main {
public static void main(String[] argv) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
NamedNodeMap attrs = element.getAttributes();
String[] names = new String[attrs.getLength()];
for (int i = 0; i < names.length; i++) {
names[i] = attrs.item(i).getNodeName();
}
for (int i = 0; i < names.length; i++) {
attrs.removeNamedItem(names[i]);
}
}
}
Removing All the Attributes in a DOM Element
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
public class Main {
public static void main(String[] argv) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
NamedNodeMap attrs = element.getAttributes();
while (attrs.getLength() > 0) {
attrs.removeNamedItem(attrs.item(0).getNodeName());
}
}
}