下面列出了org.w3c.dom.Text#getNodeValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets the text data contained inside a tag.
*
* @param parent the parent of the tag containing the text
* @param elementName the name of the tag containing the text
* @return the text contents
*/
protected String getChildTextValue(Element parent, String elementName) {
Element child = getChildElement(parent, elementName);
assertTrue(child.hasChildNodes());
NodeList children = child.getChildNodes();
int length = children.getLength();
assertTrue(length > 0);
// The children may be a sucession of text elements, just concatenate them
String result = "";
for (int i = 0; i < length; i++) {
Text textNode = (Text) children.item(i);
result += textNode.getNodeValue();
}
return result;
}
/**
* Extract nested text from an element.
* Currently does not handle coalescing text nodes, CDATA sections, etc.
* @param parent a parent element
* @return the nested text, or null if none was found
*/
static String findText(Element parent) {
NodeList l = parent.getChildNodes();
for (int i = 0; i < l.getLength(); i++) {
if (l.item(i).getNodeType() == Node.TEXT_NODE) {
Text text = (Text)l.item(i);
return text.getNodeValue();
}
}
return null;
}
/**
* Extract nested text from a node.
* Currently does not handle coalescing text nodes, CDATA sections, etc.
* @param parent a parent element
* @return the nested text, or null if none was found
*
* @since 8.4
*/
public static String findText(Node parent) {
NodeList l = parent.getChildNodes();
for (int i = 0; i < l.getLength(); i++) {
if (l.item(i).getNodeType() == Node.TEXT_NODE) {
Text text = (Text) l.item(i);
return text.getNodeValue();
}
}
return null;
}
@Test
public void testAddRuntimeAtEnd() {
Document transformed = transform("/xslt/addJava8Runtime.xsl",
"<appengine-web-app xmlns=\"http://appengine.google.com/ns/1.0\"><threadsafe>true</threadsafe></appengine-web-app>");
Element runtime = getRuntimeElement(transformed);
assertEquals("java8", runtime.getTextContent());
Text previous = (Text) runtime.getPreviousSibling();
assertTrue(previous.getNodeValue().trim().isEmpty());
Text next = (Text) runtime.getNextSibling();
String s = next.getNodeValue();
assertEquals("\n", s);
}
public void testText(Text text) {
String fullText = text.getNodeValue();
if (fullText.startsWith("bar")) {
Assert.assertFalse("testText called", textCalled);
}
if (!"barhelloxyzzy".equals(fullText)) {
if (!textCalled) {
Assert.assertEquals("bar", fullText);
} else {
Assert.assertEquals("helloxyzzy", fullText);
}
} // else - parser didn't expand entity reference
textCalled = true;
}
/**
* Creates the GatewayResponse object by parsing an xml from a stream. Fills in
* the fields of the object that are available through getters after this method
* returns.
*
* @param xmlstream
* the stream to parse the response from
* @throws Exception
* if the xml contains a root element with a bad name or an unknown
* element, or if the xml is badly formatted
*/
public GatewayResponse(InputStream xmlstream, GatewayRequest req) throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(xmlstream);
// get the root node
Node rootnode = doc.getDocumentElement();
String root = rootnode.getNodeName();
//if (root != "ewayResponse") { // SCIPIO: bad
if (!"ewayResponse".equals(root)) {
throw new Exception("Bad root element in response: " + root);
}
// get all elements
NodeList list = doc.getElementsByTagName("*");
int length = list.getLength();
for (int i = 0; i < length; i++) {
Node node = list.item(i);
String name = node.getNodeName();
//if (name == "ewayResponse") { // SCIPIO: bad
if ("ewayResponse".equals(name)) {
continue;
}
Text textnode = (Text) node.getFirstChild();
String value = "";
if (textnode != null) {
value = textnode.getNodeValue();
}
// SCIPIO: 2018-09-26: Added break statements missing from upstream
switch(name) {
case "ewayTrxnError":
txTrxnError = value;
break;
case "ewayTrxnStatus":
if ("true".equals(value.toLowerCase(Locale.getDefault()).trim())) {
txTrxnStatus = true;
}
break;
case "ewayTrxnNumber":
txTrxnNumber = value;
break;
case "ewayTrxnOption1":
txTrxnOption1 = value;
break;
case "ewayTrxnOption2":
txTrxnOption2 = value;
break;
case "ewayTrxnOption3":
txTrxnOption3 = value;
break;
case "ewayReturnAmount":
if (!value.equals("")) {
txReturnAmount = Integer.parseInt(value);
}
break;
case "ewayAuthCode":
txAuthCode = value;
break;
case "ewayTrxnReference":
txTrxnReference = value;
break;
case "ewayBeagleScore":
if (!value.equals("")) {
txBeagleScore = Double.parseDouble(value);
}
break;
default:
throw new Exception("Unknown field in response: " + name);
}
}
if (req.isTestMode()) {
Debug.logInfo("[eWay Reply]\n" + this.toString(), module);
}
}
public static XMLNode convert( Node n )
{
if ( n == null )
{
return null;
}
else if ( n instanceof Text )
{
Text tn = (Text) n;
return new XMLNode( tn.getNodeValue() );
}
else if ( n instanceof Element )
{
Element en = (Element) n;
XMLAttribute xmlatts = null;
NamedNodeMap attributes = en.getAttributes();
for ( int i = attributes.getLength() - 1; i >= 0; i-- )
{
Attr ar = (Attr) attributes.item( i );
xmlatts = new XMLAttribute( ar.getName(), ar.getValue(), xmlatts );
}
// Convert childern
XMLNode thisNode = new XMLNode( en.getNodeName(), xmlatts, null, null );
;
XMLNode last = null;
Node nn = en.getFirstChild();
while ( nn != null )
{
if ( thisNode.getNested() == null )
{
last = convert( nn );
thisNode.setNested( last );
}
else
{
XMLNode nnode = convert( nn );
last.setNext( nnode );
last = nnode;
}
last.setParent( thisNode );
nn = nn.getNextSibling();
}
return thisNode;
}
return null;
}