![]() It’s an advanced step, and I can’t think of anything in regular MATLAB that works the same way. ![]() It works similarly to Java’s regular expression engine, in that you create a string that represents nodes you want to match, compile that to an internal representation and then evaluate it on your document. XPath is a language for finding nodes in an XML document, and comes with Java. This then returns a list of matching nodes, which we can iterate, but since I know there’s only one PhoneNumber I just grabbed the 0’th element: phoneNumber = friendlyInfo.getElementsByTagName( 'PhoneNumber').item(0).getTextContent Instead of iterating to find the PhoneNumber node, we can use the ElementsByTagName method to find all the elements in the subtree that have a certain name. If strcmpi(em(i).getTagName, 'PhoneNumber') Note that since this is a Java array, the array indices go from 0 to size-1. Note that if there are multiple PhoneNumber child nodes of the Entry, this will stop after finding the first one.Īnother way to iterate over the children is to use item() method. Once I have the desired node, I used the getTextContent() method to get the text inside the tags. I used the getNodeName() method to get the string value of the node in order to compare it with “PhoneNumber.” If you’re looking at the methods of a node, the getNodeName() method is redundant with the getTagName() method. Then using the getNextSibling() method, I can walk through all the other child nodes to find the one I’m looking for, in this case it’s PhoneNumber. In the above example I used getFirstChild() which returns the first child (in this case, the Name node). There are several ways to navigate the returned node list. The getChildNodes() method returns a list of nodes. If strcmpi(node.getNodeName, 'PhoneNumber') % Iterate over the nodes to find the "PhoneNumber" % once there are no more siblinings, "node" will be empty % Get the first "Entry"'s children % Remember that java arrays are zero-basedįriendlyInfo = em(0).getChildNodes % Get the "AddressBook" nodeĪddressBookNode = docNode.getDocumentElement To do this I’m going to start the root node, “ AddressBook.” From there I will walk down the tree to AddressBook/Entry/PhoneNumber and get the the text of the PhoneNumber node. Let’s say I want to find Friendly’s phone number. The first way makes use of the structure of the tree and relationship of the nodes, the second uses the XPath language to precisely pick out a node. Both of the ways I describe here once again take advantage of the Java environment that runs with MATLAB. There are at least two ways to navigate the tree in MATLAB. Remember, you can create a new tree or read one into MATLAB using the xmlwrite function.įor your reference, here are the other parts in the series: This week I answer the question:” now that I have a tree, how can I extract data from it?” I’ll continue to use the AddressBook example from the last post. Last time in my XML in MATLAB series I explained the steps needed to create an XML DOM structure and build up an XML tree. We’ll be back to the new MATLAB R2010b features next week. Since I’ve had a request to cover this topic, I’ve moved it up in the schedule. This week I’m posting the third part in my series on using XML.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |