In this discussion about dom vs sax here
the chosen answer says:
DOM is easier to use but has an overhead of parsing the entire XML before you can start using it
I understood that in SAX, you dont need to parse the whole xml.
Let us say that i am using Sax parser and i want to find a particular node. Now if the node is towards the end of the xml document, how will the sax parser find it without parsing the whole xml?
My other question is why in sax we cannot insert/delete a node?
For some reason these answers are not obvious from the statement "sax is event based".
Both Dom and Sax method read the all file!
It just mean that Dom is parsing every node so that it create a tree in memory where you can get what you want using a Dom request.
Sax is different, it doesn't parse the file, it will just notify you with call back after each event that happen while reading it instead, like: startDocument, startElement, endElement, endDocument... in those method you can check the name of each tag/attribute for exemple and just pickup what you are interested in.
Related
We are adding code to convert a DOM to Domino before invoking saxon library to process xquery expression, which involves constructing a xml document as output.
Following exception is seen, for DOM created using certain DOM builders:
java.lang.NullPointerException
at net.sf.saxon.om.NameOfNode.equals(NameOfNode.java:177)
at net.sf.saxon.om.SingletonAttributeMap.put(SingletonAttributeMap.java:69)
at net.sf.saxon.om.NodeInfo.attributes(NodeInfo.java:528)
at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:673)
at net.sf.saxon.om.NodeInfo.copy(NodeInfo.java:568)
at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:679)
at net.sf.saxon.om.NodeInfo.copy(NodeInfo.java:568)
at net.sf.saxon.event.ComplexContentOutputter.decompose(ComplexContentOutputter.java:860)
at net.sf.saxon.event.ComplexContentOutputter.append(ComplexContentOutputter.java:656)
The cause seems to be that node.getLocalName() for attribute type of nodes, returns null for Domino. Same setup works if DOM is converted to Tiny Tree or passed using a DOMWrapper to Saxon.
Xquery using domino, expected to work, threw Exception.
You've raised this on the Saxonica support forum at https://saxonica.plan.io/issues/5727
To summarise:
(a) Whether using the DOM wrapper in Saxon, or the Domino structure (which adds extra indexing), the DOM should be namespace-aware and should be constructed using namespace-aware (i.e. level-2) interfaces.
(b) If you don't follow that rule, we shouldn't crash (so we'll fix the crash that you've observed), but we can't guarantee delivering XPath-conformant results. The mapping from a non-namespace-aware DOM to a valid XDM instance is undefined and unpredictable, so the results of XPath processing are undefined. This might mean, for example, that when the result is serialized, it's not well-formed XML (for example, it might contain undeclared prefixes in element or attribute names.
I want to integrate code completion feature to CodeMirror based xml editor. It basically parses the schema of the xml file and provide code completion according to schema and its structure. But there is something i could not manage to do. For example, when the cursor is moved to a location which is inside and xml tag, code completion must behave accordingly. It should aware of that the cursor is inside the tag, etc. How can i do that?
I think XML parser of the code mirror may give a clue about semantic position of the cursor location with some alteration. Is that possible?
Or is there any generic way to analyze cursor location and behave accordingly?
Thanks in advance.
If you use CodeMirror 2, there's a getTokenAt method that allows you to analyse the parser state at a given position. You can see this being used to autocomplete local JavaScript variables in http://codemirror.net/2/demo/complete.html . For the XML parser, you can inspect the context property of the state, which is a linked list of objects, each containing a tagName property and a prev property linking to the context above it.
I want to create a XML editor (form view type) for iphone. Can anyone suggest me how to proceed in this regard. I know how to parse an XML document but I am having trouble in editing the contents of the file dynamically. This editor should be such that, a new node can be added or a node can be deleted, it can also edit the values.
Please help...
In short you need to parse the document into a readable structure, finding a way to modify that structure, and write it back to XML.
Classes you are likely to need to write include MyXMLDocument, MyXMLNode, MyXMLElement and MyXMLTextNode, and MyXMLNode likely needs NSArray *children;, MyXMLNode *parent; and MyXMLDocument *document; as ivars.
You also probably need something akin to a -stringValue method; and an understanding of how the XML DOM usually works.
In my application, I have a tag multiple times. I'm using xml parser. I'm taking a corresponding element with similar name as the one in xml file in my class. So in case of:
<photo>abc</photo>
<photo>def</photo>
What I get in photo element of my class is the second element i.e def, as the first one gets overwritten as there's only one photo element in my class. My question is am I wrong in taking similar elements in class as in case of xml? Is there any better method or a better parser? Or I'm on right path and have to do this manually by setting some flags etc?
Thanx in advance.
I assume you try to parse the contents of an XML to match to properties on an object using NSXMLParser.
If it is valid to encounter multiple photo tags in your XML then either you need a strategy to know which one will map to a property, or convert your property to an NSArray and add the results as they come.
If it is not a valid case, you could check if you set the property earlier and raise an error, or just override it (as you do) and call it "undefined behavior".
There are many parsers there that can handle your issue (e.g. XPathQuery, TouchXML etc.).
I don't think that there is a need to reinvent the wheel - use one of the existing parsers.
i am able to parse the XML file. but i am facing strange error. My XML file format is like this
<contact>
<contactServiceTag>3B92E6A7-B599-EAE9-1CB7B5592CB8695C</contactServiceTag>
<contactDeletedBoolean>0</contactDeletedBoolean>
<contactLinkStatus>Stored</contactLinkStatus>
<contactName>P4</contactName>
−
<contactEmailAddresses>
<workEmail>updatedp4#isol.co.in</workEmail>
<personalEmail/>
<otherEmail/>
</contactEmailAddresses>
<contactLastUpdated>{ts '2010-01-22 10:05:42'}</contactLastUpdated>
<contactPhotoExists>False</contactPhotoExists>
</contact>
during the parsing, when parser parse the element contactLastUpdated , then foundCharacters method called multiple time and it return the value {ts on first run, \' on second run, 2010-01-22 10:05:42 on third run,\' on fourth run and finally } on last run. so i get only last value (}) when i called didEndElement method.
please suggest how can i resolve this type of error
In your implementation of the <NSXMLParserDelegate> callbacks like parser:foundCharacters:, you should be storing the found characters in instance variables, possibly concatenating a string together, so that when parser:didEndElement:namespaceURI:qualifiedName: is invoked, you have the full element value/body available to your object through its instance variable state.
You might also read up on the difference between SAX and DOM parsers. NSXMLParser is a SAX parser which is less convenient to use, but performs better than DOM parsers.
Create a string when entering an element, append to it when foundCharacters is called and then check its length/value on didEndElement?
Both Jon's and Mobs' answers are correct, that is the way to do it. In order to understand better how it works, I suggest that you take a look at Apple's Seismic XML sample project. It uses the NSXMLParser in a very clear way and also shows how to handle the situation you are in.