本文整理汇总了Java中org.w3c.dom.Element.setAttributeNode方法的典型用法代码示例。如果您正苦于以下问题:Java Element.setAttributeNode方法的具体用法?Java Element.setAttributeNode怎么用?Java Element.setAttributeNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.w3c.dom.Element
的用法示例。
在下文中一共展示了Element.setAttributeNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createAttribute
import org.w3c.dom.Element; //导入方法依赖的package包/类
@Override
public DomNode createAttribute(DomNode parent, QName attribute) throws XmlBuilderException {
final org.w3c.dom.Node parentNode = parent.getNode();
if (org.w3c.dom.Node.ELEMENT_NODE != parentNode.getNodeType()) {
throw new XmlBuilderException("Unable to append attribute to a non-element node " + parent);
}
try {
Attr attr;
final Element parentElement = (Element) parentNode;
if (XMLConstants.NULL_NS_URI.equals(attribute.getNamespaceURI())) {
attr = document.createAttribute(attribute.getLocalPart());
} else {
attr = document.createAttributeNS(attribute.getNamespaceURI(), attribute.getLocalPart());
attr.setPrefix(attribute.getPrefix());
}
parentElement.setAttributeNode(attr);
return new DomNode(attr);
} catch (DOMException de) {
throw new XmlBuilderException("Unable to create attribute: " + attribute, de);
}
}
示例2: createAndAppendAttribute
import org.w3c.dom.Element; //导入方法依赖的package包/类
private Attr createAndAppendAttribute(
@NotNull final Element owner,
@NotNull final Attributes attrs,
final int idx
) {
final String localName = attrs.getLocalName(idx);
final Attr result;
if (StringUtil.isEmpty(localName)) {
result = this.myDoc.createAttribute(attrs.getQName(idx));
owner.setAttributeNode(result);
} else {
result = this.myDoc.createAttributeNS(attrs.getURI(idx), localName);
owner.setAttributeNodeNS(result);
}
return result;
}
开发者ID:AlexanderBartash,项目名称:hybris-integration-intellij-idea-plugin,代码行数:17,代码来源:ValidateContextImpl.java
示例3: getObjectAsElement
import org.w3c.dom.Element; //导入方法依赖的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;
}
示例4: assignAttributeValue
import org.w3c.dom.Element; //导入方法依赖的package包/类
/**
* Sets the text value of an attribute on a provided element if set in the attributeValueMap table.
*
* @param attName the attribute local name.
* @param contentElem the element to set the attribute on.
*/
public void assignAttributeValue(String attName,
Element contentElem) {
if (null != attName && null != contentElem) {
String attValue = null;
if (sampleXML) {
attValue = "?";
}
else {
attValue = getAttributeValue(attName, contentElem);
}
if (null != attValue) {
Attr att = DocumentHelper.createAttribute(generatedDoc, "", attName);
att.setValue(attValue);
contentElem.setAttributeNode(att);
}
}
}
示例5: testDuplicateAttributeNode
import org.w3c.dom.Element; //导入方法依赖的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);
}
}
示例6: createNodeProperty
import org.w3c.dom.Element; //导入方法依赖的package包/类
/**
* Create a property XML Node
*/
private Element createNodeProperty(Property p){
Element el = getDocument().createElement("property");
Attr name = getDocument().createAttribute("name");
name.setValue(p.getName());
el.setAttributeNode(name);
Attr value = getDocument().createAttribute("value");
value.setValue(p.toString());
el.setAttributeNode(value);
return el;
}
示例7: createNodeEdge
import org.w3c.dom.Element; //导入方法依赖的package包/类
/**
* Create Node to XML from Edge Graph
*/
private org.w3c.dom.Node createNodeEdge(int i, int j) {
Edge e = xmlGraph.getEdge(i, j);
// edge
Element graphEdge = getDocument().createElement("edge");
Attr v1 = getDocument().createAttribute("v1");
v1.setValue((new Integer(i)).toString());
graphEdge.setAttributeNode(v1);
Attr v2 = getDocument().createAttribute("v2");
v2.setValue((new Integer(j)).toString());
graphEdge.setAttributeNode(v2);
// properties
Element xmlProperties = getDocument().createElement("properties");
ComponentProperties prop = e.getProperties();
EdgeWeight edgeweight = (EdgeWeight) prop.getPropertyByName("EdgeWeight");
xmlProperties.appendChild(createNodeProperty(edgeweight));
ComponentColor cc = (ComponentColor) prop.getPropertyByName("ComponentColor");
xmlProperties.appendChild(createNodeProperty(cc));
graphEdge.appendChild(xmlProperties);
return graphEdge;
}
示例8: saveTranscription
import org.w3c.dom.Element; //导入方法依赖的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;
}
示例9: append
import org.w3c.dom.Element; //导入方法依赖的package包/类
private static void append(Element parent, Node node) {
if (parent != null) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
parent.appendChild(node);
} else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
parent.setAttributeNode((Attr) node);
}
}
}
示例10: testWellFormed001
import org.w3c.dom.Element; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the attribute has EntityReference to '<', <br>
* <b>name</b>: well-formed <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: An error is reported
*/
@Test
public void testWellFormed001() {
Document doc = null;
try {
doc = loadDocument(null, test2_xml);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("well-formed", Boolean.TRUE)) {
Assert.fail("setting 'well-formed' to true is not supported");
}
config.setParameter("well-formed", Boolean.TRUE);
Element root = doc.getDocumentElement();
Attr attr = doc.createAttributeNS(null, "attr");
try {
attr.appendChild(doc.createEntityReference("<"));
} catch (DOMException domException) {
System.out.println("testWellFormed001: Expected DOMException for Attribute value = '<'" + domException.toString());
return; // OK
}
root.setAttributeNode(attr);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
doc.normalizeDocument();
if (testHandler.getError() == null && null == testHandler.getFatalError()) {
Assert.fail("no error was reported when attribute has <");
}
return; // Status.passed("OK");
}
示例11: testWellFormed002
import org.w3c.dom.Element; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the attribute has EntityReference to '<', <br>
* <b>name</b>: well-formed <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: No error is reported
*/
@Test
public void testWellFormed002() {
Document doc = null;
try {
doc = loadDocument(null, test2_xml);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("well-formed", Boolean.FALSE)) {
System.out.println("OK, setting 'well-formed' to false is not supported");
return;
}
config.setParameter("well-formed", Boolean.FALSE);
Element root = doc.getDocumentElement();
Attr attr = doc.createAttributeNS(null, "attr");
attr.appendChild(doc.createEntityReference("x"));
root.setAttributeNode(attr);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
doc.normalizeDocument();
if (testHandler.getError() != null || null != testHandler.getFatalError()) {
Assert.fail("unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError());
}
return; // Status.passed("OK");
}
示例12: testAddNewAttributeNode
import org.w3c.dom.Element; //导入方法依赖的package包/类
@Test
public void testAddNewAttributeNode() throws Exception {
Document document = createDOMWithNS("DocumentTest01.xml");
NodeList nodeList = document.getElementsByTagNameNS("http://www.w3.org/TR/REC-html40", "body");
NodeList childList = nodeList.item(0).getChildNodes();
Element child = (Element) childList.item(1);
Attr a = document.createAttributeNS("urn:BooksAreUs.org:BookInfo", "attributeNew");
child.setAttributeNode(a);
assertNotNull(child.getAttributeNodeNS("urn:BooksAreUs.org:BookInfo", "attributeNew"));
}
示例13: testCreateNewUser
import org.w3c.dom.Element; //导入方法依赖的package包/类
/**
* Checking when creating an XML document using DOM Level 2 validating
* it without having a schema source or a schema location It must throw a
* sax parse exception.
*
* @throws Exception If any errors occur.
*/
@Test
public void testCreateNewUser() throws Exception {
String resultFile = USER_DIR + "accountInfoOut.xml";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
MyErrorHandler eh = new MyErrorHandler();
docBuilder.setErrorHandler(eh);
Document document = docBuilder.newDocument();
Element account = document.createElementNS(PORTAL_ACCOUNT_NS, "acc:Account");
Attr accountID = document.createAttributeNS(PORTAL_ACCOUNT_NS, "acc:accountID");
account.setAttributeNode(accountID);
account.appendChild(document.createElement("FirstName"));
account.appendChild(document.createElementNS(PORTAL_ACCOUNT_NS, "acc:LastName"));
account.appendChild(document.createElement("UserID"));
DOMImplementationLS impl
= (DOMImplementationLS) DOMImplementationRegistry
.newInstance().getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
try(FileOutputStream output = new FileOutputStream(resultFile)) {
MyDOMOutput domOutput = new MyDOMOutput();
domOutput.setByteStream(output);
writer.write(account, domOutput);
docBuilder.parse(resultFile);
}
assertTrue(eh.isAnyError());
}
示例14: createScreenClassChildElement
import org.w3c.dom.Element; //导入方法依赖的package包/类
private static Element createScreenClassChildElement(Document document, DatabaseObject dbo, DatabaseObject dboParent) throws DOMException, Exception {
Element elt = createDboElement(document, dbo, !dbo.getDatabaseObjectChildren().isEmpty());
elt.setAttributeNode(createIsInheritedAttr(document, dbo, dboParent));
return elt;
}
示例15: startElement
import org.w3c.dom.Element; //导入方法依赖的package包/类
public void startElement(QName element, XMLAttributes attributes,
Augmentations augs) throws XNIException {
Element elem;
int attrCount = attributes.getLength();
if (fDocumentImpl == null) {
elem = fDocument.createElementNS(element.uri, element.rawname);
for (int i = 0; i < attrCount; ++i) {
attributes.getName(i, fAttributeQName);
elem.setAttributeNS(fAttributeQName.uri, fAttributeQName.rawname, attributes.getValue(i));
}
}
// If it's a Xerces DOM store type information for attributes, set idness, etc..
else {
elem = fDocumentImpl.createElementNS(element.uri, element.rawname, element.localpart);
for (int i = 0; i < attrCount; ++i) {
attributes.getName(i, fAttributeQName);
AttrImpl attr = (AttrImpl) fDocumentImpl.createAttributeNS(fAttributeQName.uri,
fAttributeQName.rawname, fAttributeQName.localpart);
attr.setValue(attributes.getValue(i));
// write type information to this attribute
AttributePSVI attrPSVI = (AttributePSVI) attributes.getAugmentations(i).getItem (Constants.ATTRIBUTE_PSVI);
if (attrPSVI != null) {
if (fStorePSVI) {
((PSVIAttrNSImpl) attr).setPSVI(attrPSVI);
}
Object type = attrPSVI.getMemberTypeDefinition();
if (type == null) {
type = attrPSVI.getTypeDefinition();
if (type != null) {
attr.setType (type);
if (((XSSimpleType) type).isIDType()) {
((ElementImpl) elem).setIdAttributeNode (attr, true);
}
}
}
else {
attr.setType (type);
if (((XSSimpleType) type).isIDType()) {
((ElementImpl) elem).setIdAttributeNode (attr, true);
}
}
}
attr.setSpecified(attributes.isSpecified(i));
elem.setAttributeNode(attr);
}
}
append(elem);
fCurrentNode = elem;
if (fFragmentRoot == null) {
fFragmentRoot = elem;
}
}