本文整理汇总了Java中org.w3c.dom.Document.createAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java Document.createAttribute方法的具体用法?Java Document.createAttribute怎么用?Java Document.createAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.w3c.dom.Document
的用法示例。
在下文中一共展示了Document.createAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractFeatures
import org.w3c.dom.Document; //导入方法依赖的package包/类
public static ArrayList<Node> extractFeatures(Node n, Document D) {
ArrayList<Node> attrs = new ArrayList<Node>();
String type = n.getAttributes().getNamedItem("type").getNodeValue();
NodeList features = n.getChildNodes();
for (int i = 0; i < n.getChildNodes().getLength(); i++) {
if (n.getChildNodes().item(i) instanceof Element) {
features = n.getChildNodes().item(i).getChildNodes();
}
}
for (int i = 0; i < features.getLength(); i++) {
Node feature = features.item(i);
if (feature instanceof Element) {
String name = feature.getAttributes().getNamedItem("name")
.getNodeValue();
String value = extractFeatureValue(feature);
Node attr = D.createAttribute(type + ":" + name);
attr.setNodeValue(value);
attrs.add(attr);
}
}
return attrs;
}
示例2: duplicateNode
import org.w3c.dom.Document; //导入方法依赖的package包/类
static Node duplicateNode(Document document, Node node) {
Node newNode;
if (node.getNamespaceURI() != null) {
newNode = document.createElementNS(node.getNamespaceURI(), node.getLocalName());
} else {
newNode = document.createElement(node.getNodeName());
}
// copy the attributes
NamedNodeMap attributes = node.getAttributes();
for (int i = 0 ; i < attributes.getLength(); i++) {
Attr attr = (Attr) attributes.item(i);
Attr newAttr;
if (attr.getNamespaceURI() != null) {
newAttr = document.createAttributeNS(attr.getNamespaceURI(), attr.getLocalName());
newNode.getAttributes().setNamedItemNS(newAttr);
} else {
newAttr = document.createAttribute(attr.getName());
newNode.getAttributes().setNamedItem(newAttr);
}
newAttr.setValue(attr.getValue());
}
// then duplicate the sub-nodes.
NodeList children = node.getChildNodes();
for (int i = 0 ; i < children.getLength() ; i++) {
Node child = children.item(i);
if (child.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
Node duplicatedChild = duplicateNode(document, child);
newNode.appendChild(duplicatedChild);
}
return newNode;
}
示例3: addAttribute
import org.w3c.dom.Document; //导入方法依赖的package包/类
static void addAttribute(Document document, Node node,
String namespaceUri, String attrName, String attrValue) {
Attr attr;
if (namespaceUri != null) {
attr = document.createAttributeNS(namespaceUri, attrName);
} else {
attr = document.createAttribute(attrName);
}
attr.setValue(attrValue);
if (namespaceUri != null) {
node.getAttributes().setNamedItemNS(attr);
} else {
node.getAttributes().setNamedItem(attr);
}
}
示例4: processSingleNodeNamespace
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* Update the namespace of a given node to work with a given document.
*
* @param node the node to update
* @param document the new document
*
* @return false if the attribute is to be dropped
*/
private static boolean processSingleNodeNamespace(Node node, Document document) {
if ("xmlns".equals(node.getLocalName())) {
return false;
}
String ns = node.getNamespaceURI();
if (ns != null) {
NamedNodeMap docAttributes = document.getAttributes();
String prefix = getPrefixForNs(docAttributes, ns);
if (prefix == null) {
prefix = getUniqueNsAttribute(docAttributes);
Attr nsAttr = document.createAttribute(prefix);
nsAttr.setValue(ns);
document.getChildNodes().item(0).getAttributes().setNamedItem(nsAttr);
}
// set the prefix on the node, by removing the xmlns: start
prefix = prefix.substring(6);
node.setPrefix(prefix);
}
return true;
}
示例5: getObjectAsElement
import org.w3c.dom.Document; //导入方法依赖的package包/类
@Override
public Element getObjectAsElement(Document doc, Element rootElement) throws Exception
{
Element calendarRoot = doc.createElement("calendar");
rootElement.appendChild(calendarRoot);
//Name
Attr name = doc.createAttribute("name");
name.setValue(getName());
calendarRoot.setAttributeNode(name);
//Default category
Element defCatE = doc.createElement("default_category");
defCatE.appendChild(doc.createTextNode(defaultCategory.getName()));
calendarRoot.appendChild(defCatE);
//Categories
for(Category c : categories)
calendarRoot.appendChild(c.getObjectAsElement(doc, rootElement));
//Entries
for(Entry e : entries)
calendarRoot.appendChild(e.getObjectAsElement(doc, rootElement));
return calendarRoot;
}
示例6: testDuplicateAttributeNode
import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test
public void testDuplicateAttributeNode() throws Exception {
final String name = "testAttrName";
final String value = "testAttrValue";
Document document = createNewDocument();
Attr attr = document.createAttribute(name);
attr.setValue(value);
Element element1 = document.createElement("AFirstElement");
element1.setAttributeNode(attr);
Element element2 = document.createElement("ASecondElement");
Attr attr2 = (Attr) attr.cloneNode(true);
element2.setAttributeNode(attr2);
assertEquals(element1.getAttribute(name), element2.getAttribute(name));
Element element3 = document.createElement("AThirdElement");
try {
element3.setAttributeNode(attr);
fail(DOMEXCEPTION_EXPECTED);
} catch (DOMException doe) {
assertEquals(doe.code, INUSE_ATTRIBUTE_ERR);
}
}
示例7: testSetNamedItem
import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test
public void testSetNamedItem() throws Exception {
Document document = createDOMWithNS("NamedNodeMap03.xml");
NodeList nodeList = document.getElementsByTagName("body");
nodeList = nodeList.item(0).getChildNodes();
Node n = nodeList.item(1);
NamedNodeMap namedNodeMap = n.getAttributes();
Attr attr = document.createAttribute("name");
Node replacedAttr = namedNodeMap.setNamedItem(attr);
assertEquals(replacedAttr.getNodeValue(), "attributeValue");
Node updatedAttrNode = namedNodeMap.getNamedItem("name");
assertEquals(updatedAttrNode.getNodeValue(), "");
Attr newAttr = document.createAttribute("nonExistingName");
assertNull(namedNodeMap.setNamedItem(newAttr));
Node newAttrNode = namedNodeMap.getNamedItem("nonExistingName");
assertEquals(newAttrNode.getNodeValue(), "");
}
示例8: extractFeatureAtts
import org.w3c.dom.Document; //导入方法依赖的package包/类
public static ArrayList<Node> extractFeatureAtts(Fs features, Document D,
String prefix) {
ArrayList<Node> atts = new ArrayList<Node>();
for (String key : features.getKeys()) {
String value = features.getFeat(key).toString();
Attr att = D.createAttribute(prefix + key);
att.setNodeValue(value);
atts.add(att);
}
return atts;
}
示例9: saveTranscription
import org.w3c.dom.Document; //导入方法依赖的package包/类
public void saveTranscription(File transcriptionFile)
throws ParserConfigurationException, TransformerException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("transcription");
doc.appendChild(rootElement);
// Text Element
Element textE = doc.createElement("text");
textE.appendChild(doc.createTextNode(text.getText()));
rootElement.appendChild(textE);
// Timestamps
Element timeStamps = doc.createElement("time_stamps");
rootElement.appendChild(timeStamps);
for (int i = 0; i < text.getStyleRanges().length; i++) {
StyleRange range = text.getStyleRanges()[i];
Element timeStampE = doc.createElement("timeStamp");
Attr start = doc.createAttribute("start");
Attr lenght = doc.createAttribute("lenght");
start.setValue("" + range.start);
lenght.setValue("" + range.length);
timeStampE.setAttributeNode(start);
timeStampE.setAttributeNode(lenght);
timeStamps.appendChild(timeStampE);
}
// Save the file
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(transcriptionFile);
transformer.transform(source, result);
changed = false;
}
示例10: createAttribute
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* Static method to create a DOM attribute.
*
* @param aDoc is the document
* @param asNameSpaceURI is the name space for the attribute. Will be null for DOM level 1 attribute
* @param asLocalName is the attribute name
*/
public static Attr createAttribute(Document aDoc,
String asNameSpaceURI,
String asLocalName) {
Attr newAttr = null;
newAttr = aDoc.createAttribute(asLocalName);
return newAttr;
}
示例11: testGetName
import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test
public void testGetName() throws Exception {
Document document = createDOM("Attr01.xml");
//test a new created Attr
Attr attr = document.createAttribute("newAttribute");
assertEquals(attr.getName(), "newAttribute");
//test a Attr loaded from xml file
Element elemNode = (Element) document.getElementsByTagName("book").item(1);
Attr attr2 = (Attr) elemNode.getAttributes().item(0);
assertEquals(attr2.getName(), "category1");
}
示例12: testNewCreatedAttribute
import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test
public void testNewCreatedAttribute() throws Exception {
Document document = createDOM("Attr01.xml");
Attr attr = document.createAttribute("newAttribute");
assertTrue(attr.getSpecified());
assertNull(attr.getOwnerElement());
}
示例13: testSetValue
import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test
public void testSetValue() throws Exception {
Document document = createDOM("Attr01.xml");
Attr attr = document.createAttribute("newAttribute");
attr.setValue("newVal");
assertEquals(attr.getValue(), "newVal");
}
示例14: testSetAttributeNode
import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test
public void testSetAttributeNode() throws Exception {
final String attrName = "myAttr";
final String attrValue = "attrValue";
Document document = createDOM("ElementSample02.xml");
Element elemNode = document.createElement("pricetag2");
Attr myAttr = document.createAttribute(attrName);
myAttr.setValue(attrValue);
assertNull(elemNode.setAttributeNode(myAttr));
assertEquals(elemNode.getAttribute(attrName), attrValue);
}
示例15: createIsInheritedAttr
import org.w3c.dom.Document; //导入方法依赖的package包/类
private static Attr createIsInheritedAttr(Document document, DatabaseObject dbo, DatabaseObject dboParent) {
Attr attr = document.createAttribute("isInherited");
attr.setNodeValue(Boolean.toString(!dboParent.toString().equals(dbo.getParent().toString())));
return attr;
}