Java-XPath (Code)

xiaoxiao2021-03-06  25

/ *

* CREATED ON 2005/01/24

*

* Introduce XPath

* /

Package org.brunt.xml.xpath;

Import java.io.stringbufferinputstream;

Import javax.xml.parsers.documentBuilderFactory;

Import org.apache.xpath.xpathapi;

Import org.w3c.dom.document;

Import Org.w3c.dom.Node;

Import org.w3c.dom.traversal.nodeiterator;

Import org.xml.sax.inputsource;

/ **

*

@Author HT

*

* Test XPath

* /

Public class testxpath {

Public Static Final String Description =

"

http://www.zvon.org/xxl/xpathtutorial/general_chi/examples.html ";

Public static firm string xpath [] = {

// eXample1: start with '/' Express a absoulte path

"/ Aaa", "/ aaa / xxx / ddd", "/ aaa / xxx / dddno",

// eXample2: start with '//' Express All ELEMENT

"// bbb", "// bbb / bbb", "// bbb / bbbno",

// eXample3: '*' Express All

"/ AAA / XXX / DDD / *", "// DDD / *",

// eXample4: '[]' Descript position

"/ AAA / XXX / DDD / BBB [1]",

"/ AAA / XXX / DDD / BBB [2]",

"/ AAA / XXX / DDD / BBB [Last ()]", // last ()

// EXAMPLE5:

'@' Descript Attribute

"@ Name",

// "@ id",

"/ Aaa / @ id",

"// DDD / @ Name",

"// ddd / @ color",

"// bbb [

@ *] ",

"// bbb [NOT

@ *)] ", // NOT ()

// EXAMPLE6:

"// bbb [

@ color = 'Yellow'] ",

"// bbb [

@ color = 'red'] ",

"// bbb [Normalize-Space

@color) = 'red'] ", // Normalize-Space ()

// eXample7: count ()

"// * [count (bbb) = 3]", // count ()

"// * [count (*) = 1]", // *

// Example8: name () starts-with () Contains ()

"// * [Name () = 'bbb']", // name ()

"// * [starts-with (name (), 'd')]", // starts-with () "// * [contains (name (), 'e')], //com ()

// eXample9:

"// * [String-length (name ()) = 5]", // string-length ()

"// * [string-length (name ()) <5]",

// eXample10: Connect with some path by '|'

"// bbb [

@ ID = 'b1'] | // eee | // fff ",

// eXample11:

"/ Child :: aaa", // child ::

"/ Aaa", // / child :: aaa == aaa

// eXample12:

"/ AAA / XXX / DDD / DESCENDANT :: *", // Descendant ::

"// ddd / descendant :: *",

// eXample13:

"// bbb / parent :: *", // parent ::

// eXample14:

"/ AAA / XXX / DDD / BBB / ANCESTOR :: *", // Ancestor ::

"/ AAA / XXX / DDD / BBB / ANCESTOR :: XXX",

// eXample15:

"/ AAA / XXX / FOLLOWING-SIBLING :: *", // FOLLOWING-SIBLING ::

// eXample16:

"/ AAA / CCC / Preceding-Sibling :: *", // Preceding-Sibling ::

// eXample17:

"/ AAA / XXX / FOLLOWING :: *", // FOLLOWING ::

// eXample18:

"/ Aaa / xxx / preceding :: *", // preceding ::

// eXample19:

"/ AAA / XXX / Descendant-OR-Self :: *", // Descendant-OR-Self ::

// eXample20:

"/ AAA / XXX / DDD / EEE / Ancestor-OR-Self :: *", ///iantestor-or-self ::

// eXample21:

"// bb / self :: *",

"// eee / networkster :: * | // eee / descendant :: * | // eee / preceding :: * | // eee / self :: *",

// eXample22:

"// bbb [position () MOD 2 = 0]", // position () MOD

"// bbb [position () = floor (Last () Div 2 0.5) or position () = ceiling (last () DIV 2 0.5)]",

}

Public static final string default_xml_string =

" / n"

" / n"

" / N" " / n"

" / n"

" / n"

" / n"

" / n"

" / n"

" hello / n"

" / n"

" / N"

" / N"

" / n"

" / N"

" / n"

" / n"

" / n"

" / n"

" / N"

" / N"

" / n"

" / n"

" / n"

" / n"

" / n"

" / n"

" / n"

" / n"

" / n"

" / n"

" / n"

" / n";

Public static void printDefaultxml () {

System.out.println (Default_Xml_string);

Public static infutsource getDefaultinputsource () {

Return New InputSource (New StringBufferInputStream (default_xml_string);

}

Public Static Document GetDocument (InputSource IS) THROWS Exception {

DocumentBuilderFactory DFActory = DocumentBuilderFactory.newinstance ();

DFActory.setNamespaceaware (TRUE);

Return DFActory.NewDocumentBuilder (). Parse (IS);

}

Private static void printnodeiterator (nodeiterator ni) {

Node node = NULL;

While ((node ​​= ni.nextNode ())! = null) {

IF (node.getnodetype () == node.ement_node) {

System.out.print ("/ t [nodename]:" node.getnodename ());

System.out.print ("/ T [NodeValue]:" node.getnodevalue ());

PrintNodeAttrid (Node);

} else if (node.getnodetype () == node.attribute_node) {

System.out.println (

"/ t / t [attr]:"

node.getnodename ()

"="

node.getnodeValue ());

}

}

}

Private static void printnodettrid (node ​​node) {

String id = "null";

IF (node.hasettributes ()) {

Node AttrNode = node.getattributes (). GetnamedItem ("ID");

IF (attrnode! = NULL)

ID = attrnode.getnodeValue ();

}

System.out.println ("/ T / T [NodeID]:" ID);

}

Public void run () throws exception {

Run (NULL);

}

Public void run (inputsource is) throws exception {

Document doc = getDocument (is == null? GetDefaultinputsource (): IS);

For (int i = 0; i

Run (XPath [I], DOC);

}

Public void run (string xpath, document doc) throws exception {

System.out.println ("[XPath]:" XPath);

Nodeiterator nl = xpathapi.selectNodeiterator (DOC, XPATH);

PrintNodeItrator (NL);

System.out.println ("/ n");}

Public static void main (string [] args) throws exception {

PrintDefaultXML ();

TestXPath TXPATH = New Testxpath ();

TXPATH.Run ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-65094.html

New Post(0)