Jump to content

Extract the text from XML document


nguyenman999

Recommended Posts

<?xml version="1.0" encoding="ISO-8859-1"?><bookstore><book category="COOKING"> Title <a>Everyday Italian</a> author <b>Giada De Laurentiis</b> year <c>2005</c> price <d>30.00</d></book></bookstore>I have an xml structure like that. so I would like to extract text from xml like:Title: Everyday Italianauthor: Giada De Laurentiisyear: 2005price: 30.00The problem is: Is there any way to extract the text from xml code into the structure that I given?

Link to comment
Share on other sites

Yes. XSLT (referenced from the XML, or called from JavaScript, PHP, (ASP).NET), DOM (in PHP, JavaScript, (ASP).NET, etc.), PHP's SimpleXML, "SAX" kind of interface, like a duo of XMLReader (PHP, (ASP).NET) and XMLWriter (PHP, (ASP).NET).... the list goes on and on, which is the beauty of XML.Take a pick.

Link to comment
Share on other sites

Yes. XSLT (referenced from the XML, or called from JavaScript, PHP, (ASP).NET), DOM (in PHP, JavaScript, (ASP).NET, etc.), PHP's SimpleXML, "SAX" kind of interface, like a duo of XMLReader (PHP, (ASP).NET) and XMLWriter (PHP, (ASP).NET).... the list goes on and on, which is the beauty of XML.Take a pick.
Can you explain more in detail. can we have an example for clearly!
Link to comment
Share on other sites

The links I've given you are the references of all these things. They also contain some examples. Take a look at them, and try them.Depending on what you choose to use, you can also read the corresponding W3Schools tutorial (there are ones on XSLT and DOM).

Link to comment
Share on other sites

The links I've given you are the references of all these things. They also contain some examples. Take a look at them, and try them.Depending on what you choose to use, you can also read the corresponding W3Schools tutorial (there are ones on XSLT and DOM).
yes! there is something wrong in this case. may be you misunderstand what I mean. I mean that If we have an XML file like the file that I given on top. I use it as "database" for store information about the book. then I want to manipulate on that file to extract only text about the book like that:Title: Everyday Italianauthor: Giada De Laurentiisyear: 2005price: 30.00and use this text for other purpose not display the text on browser. At the moment I use Xquery to extract text but the text under node <book> is "title author year price" so if I use the xquery like "doc("books.xml")/bookstore/book" I only get "title author year price" it is not the thing I want to get. so the problem is how can I know what is the title of the book in <a> <b> <c> <d>.
Link to comment
Share on other sites

Yeah, I forgot to mention the one thing you're using - XQuery.All of the things above, including XQuery, take an XML document as input, and produce something else as output. That output can be text, which can be used for anything, not necesarily display in a browser.Most of them, including XQuery, can use XPath for selection. In XPath, you can use text() to select a text node. Honestly, I haven't worked with XQuery, but the main idea is that you'll need to chain together several XPath statements, like:

doc("books.xml")/bookstore/book/text()[1]doc("books.xml")/bookstore/book/adoc("books.xml")/bookstore/book/text()[2]doc("books.xml")/bookstore/book/bdoc("books.xml")/bookstore/book/text()[3]doc("books.xml")/bookstore/book/cdoc("books.xml")/bookstore/book/text()[4]doc("books.xml")/bookstore/book/d

(along with literally outputting ":" after the text() calls)Alternatively, with APIs like DOM and XMLReader, you can get each node separately by chaining different method and property calls.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...