/***** Code de MesExemples.com *******/ import org.w3c.dom.Attr;import org.w3c.dom.Element;public class Utils { /** * Returns null, not "", for a nonexistent attribute. * * @param e * @param attributeName * @return */ public static String getAttributeValueEmptyNull(Element e, String attributeName) { Attr node = e.getAttributeNode(attributeName); if (node == null) { return null; } return node.getValue(); }} |
Code testé avec le fichier XML Suivant
<?xml version="1.0" encoding="windows-1252"?> <!-- Edited by MesEXemple.com --> <note> <to>Sakoba</to> <from>Adams</from> <heading>Rappel</heading> <body>Ne m'oubliez pas ce week-end!</body> </note> |
A Voir sur le même Sujet
-
Java XML: Une méthode récursive pour trouver la valeur d’un attribut Spécifique
{filelink=8368} /***** Code de MesExemples.com *******/ /** * @(#)ExempleValeurAttribut.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/4 */ import javax.xml.namespace.QName; import org.w3c.dom.Document; import org.w3c.dom.Element; import …
-
Java: Utiliser la classe ‘QName’ pour obtenir un attribut
{filelink=8362} /***** Code de MesExemples.com *******/ /** * @(#)ExempleQName_Attribut.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/4 */ import javax.xml.namespace.QName; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import …
-
Java: Utiliser la classe ‘QName’ pour obtenir un attribut
{filelink=8362} /***** Code de MesExemples.com *******/ /** * @(#)ExempleQName_Attribut.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/4 */ import javax.xml.namespace.QName; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import …
-
Java xerces: ajouter un attribut avec DOM
{filelink=8703} /***** Code de MesExemples.com *******/ import org.w3c.dom.Document;import org.apache.xerces.dom.DOMImplementation;import org.w3c.dom.Element;public class MainClass { public static void main(String args[]) { Document dom = DOMImplementation.createDocument(null, null, null); Element root = dom.createElement(“games”); Element child1 = dom.createElement(“game”); root.appendChild(child1); child1.setAttribute(“A”, “a”); }} Code testé avec le fichier XML Suivant Sakoba Adams Rappel Ne m’oubliez pas ce week-end!
-
Java: Modifier un attribut
{filelink=8358} /***** Code de MesExemples.com *******/ /** * @(#)SetAttributes.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/4 */ import javax.xml.namespace.QName; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import …
-
Java XML: Modifier un attribut
{filelink=8358} /***** Code de MesExemples.com *******/ /** * @(#)SetAttributes.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/4 */ import javax.xml.namespace.QName; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import …
-
Java XML: Lire un attribut spécifique
{filelink=8361} /***** Code de MesExemples.com *******/ /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * “License”); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;/** * Few simple utils to read DOM. This is originally from the Jakarta Commons * Modeler. * * @author Costin Manolache */public class Utils { public static String getAttribute(Node element, String attName) { NamedNodeMap attrs = element.getAttributes(); if (attrs == null) { return null; } Node attN = attrs.getNamedItem(attName); if (attN == null) { return null; } return attN.getNodeValue(); }} Code testé avec le fichier XML Suivant Sakoba Adams Rappel Ne m’oubliez pas ce week-end!
-
Java XML: Lire un attribut spécifique
{filelink=8361} /***** Code de MesExemples.com *******/ /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * “License”); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;/** * Few simple utils to read DOM. This is originally from the Jakarta Commons * Modeler. * * @author Costin Manolache */public class Utils { public static String getAttribute(Node element, String attName) { NamedNodeMap attrs = element.getAttributes(); if (attrs == null) { return null; } Node attN = attrs.getNamedItem(attName); if (attN == null) { return null; } return attN.getNodeValue(); }} Code testé avec le fichier XML Suivant Sakoba Adams Rappel Ne m’oubliez pas ce week-end!
-
Javascript: Retourner une sous-chaîne avec substr
{filelink=36} var texte = “Bienvenu sur javascript”; // extraire une partie d’un string var ssTxt = texte.substr(0,4); document.writeln(ssTxt); // retourne ‘bien’
-
XML: Utilisation de ‘StreamFilter’ pour analyser le document
{filelink=982} import java.io.*; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import javax.xml.stream.*; import javax.xml.stream.events.*; import java.util.Date; import javax.xml.namespace.QName; public class AnalyseXML implements javax.xml.stream.StreamFilter { public static final String …
-
Java: Ajouter un attribut à un élément
{filelink=8394} /***** Code de MesExemples.com *******/ /** * @(#)AddAttributToNode.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/4 */ import org.w3c.dom.Comment; import org.w3c.dom.Document; import org.w3c.dom.Element; import …
-
Javascript: retourner la valeur primitive d’un objet date
{filelink=4010} Date ValueOf
-
Java: Créer un nouveu élément et modfier son attribut*
{filelink=8426} /***** Code de MesExemples.com *******/ import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;/******************************************************************************* * Copyright (C) 2007 Google Inc. * * Licensed under the Apache License, Version 2.0 (the “License”); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an “AS IS” BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. ******************************************************************************//** * Various XML utilities. * * @author simonjsmith, ksim * @version 1.1 – ksim – March 6th, 2007 – Added functions regarding streaming * @version 1.2 – ksim – March 10th, 2007 – Added functions regarding DOM * manipulation */public class Utils { public static Element createNewElementAndSetAndAttribute(Document document, Element parent, String childElement, String childValue, String attributeName, String attributeValue) { Element child = createNewElementAndSet(document, parent, childElement, childValue); child.setAttribute(attributeName, attributeValue); return child; } public static Element createNewElementAndSet(Document document, Element parent, String childElement, String childValue) { Element child = (Element) document.createElement(childElement); parent.appendChild(child); child.setNodeValue(childValue); child.appendChild(document.createTextNode(childValue)); return child; }} Code testé avec le fichier XML Suivant Sakoba Adams Rappel Ne m’oubliez pas ce week-end!
-
Java XML: Retourner le premier élément qui a un nom local spécifique
{filelink=8472} /***** Code de MesExemples.com *******/ /** * @(#)XMLFirstElementByLocalName.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/5 */ import org.w3c.dom.Element; import org.w3c.dom.Node; public class XMLFirstElementByLocalName{ …
-
Java: Obtenir un attribut à partir d’un élément*
{filelink=8363} /***** Code de MesExemples.com *******/ /************************************************************************************ Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,* Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation** Licensed under the Educational Community License Version 1.0 (the “License”);* By obtaining, using and/or copying this Original Work, you agree that you have read,* understand, and will comply with the terms and conditions of the Educational Community License.* You may obtain a copy of the License at:** http://cvs.sakaiproject.org/licenses/license_1_0.html** THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.***********************************************************************************/import java.util.ArrayList;import java.util.List;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;public class Utils { /** * Get an Attribute from an Element. Returns an empty String if none found * @param element the containing Element * @param name the attribute name * @return Attribute as a String */ public static String getAttribute(Element element, String name) { return element.getAttribute(name); } /** * Return a list of named Elements with a specific attribute value. * @param element the containing Element * @param name the tag name * @param attribute Attribute name * @param value Attribute value * @param returnFirst Return only the first matching value? * @return List of matching elements */ public static List selectElementsByAttributeValue(Element element, String name, String attribute, String value, boolean returnFirst) { NodeList elementList = element.getElementsByTagName(name); List resultList = new ArrayList(); for (int i = 0; i
Cet article Java: Retourner nul pour un attribut non-existant* est apparu en premier sur .