How to use XML DOM API to go to all non-text nodes?

I am new to XML and DOM. I think I need to use the DOM API to find one pass through each non-text node once and print the name node.

say I got this XML example from W3C

<bookstore>

<book category="cooking">
 <title lang="en">Everyday Italian</title>
 <author>Giada De Laurentiis</author>
 <year>2005</year>
 <price>30.00</price>
 <page pagenumber="550"/>
</book>

<book category="children">
 <title lang="en">Harry Potter</title>
 <author>J K. Rowling</author>
 <year>2005</year>
 <price>29.99</price>
 <page pagenumber="500"/>
</book>
</bookstore>

I need to find a node, for example <page pagenumber="500" />, which is a non-text node

How can i do this? seduo-code will be fine too. thanks

can say

 while (x.nodeValue == NULL) {
   read the next node ?
}

I suppose that I should make myself clear, no assumptions about any documents. This should work on all XML if there is a non-text node. I assume this should be done in order from top to bottom and from left to right for each node .:(

+3
source share
4 answers

XPATH = "//* [not (text())]"
, node.
: , , .

+3

: XML-, , .

XPath, :

/bookstore/book/*[count(child::text()) = 0]

/bookstore/book/*[not(text())]

. XPath node -set, page. book, .

: , . child:: * node, text() node node , .

. : XML- ( ), , nils_gate. , .

+2

node, ? , :

  • A page
  • pagenumber 500

XPath - ( , - "DOM", DOM XPath, ).

XPath :

//page[@pagenumber='500']

XPath, , , DOM API , . , , node, , page, , pagenumber.

+1

, XPath. W3 Schools , , , node node, XPath /bookstore/book/page a node node . /bookstore/book/page[@pagenumber='500'] node, pagenumber 500.

The syntax //will find the node anywhere in the document without worrying about the structure - it can be simpler, but slower, especially with large documents. If you have a document with a known structure, it is best to use explicit XPath.

+1
source

Source: https://habr.com/ru/post/1705956/


All Articles