sax

SAX parser: Ignoring special characters

久未见 提交于 2019-11-28 01:55:21
I'm using Xerces to parse my xml document. The issue is that xml escaped characters like ' ' appear in characters() method as non-escaped ones. I need to get escaped characters inside characters() method as is. Thanks. UPD: Tried to override resolveEntity method im my DefaultHandler's descendant. Can see from debug that it's set as entity resolver to xml reader but code from overridden method is not invoked. javanna I think your solution is not too bad: a few lines of code to do exactly what you want. The problem is that startEntity and endEntity methods are not provided by ContentHandler

Is XPath much more efficient as compared to DOM and SAX?

。_饼干妹妹 提交于 2019-11-28 01:47:59
I need to parse an xml string and find values of specific text nodes, attribute values etc. I'm doing this in javascript and was using the DOMParser class for the same. Later I was informed that DOM is takes up a lot of memory and SAX is a better option. Recently I found that XPath too provides a simple way to find nodes. But I'm not sure which amongst these 3 would be the most efficient way to parse XML. Kindly help.... Björn SAX is a top-down parser and allows serial access to a XML document, and works well for read only access. DOM on the other hand is more robust - it reads the entire XML

Parsing and modifying xml string with sax parser

南笙酒味 提交于 2019-11-28 01:28:08
I have an XML file, I need to search for a specific tag in it and update it's value. The problem is that, using Sax parser is "must". I have to find these tags by using Sax Parser "only", dom stax j4dom dom4j parsers are out of consideration. Can I accomplish this task by converting my xml file to a string and parse it by using sax parser and append the new value by StringBuilder object? Would it be okay? Or what would you recommend? This is a working code, just add missing imports. It uses SAX and changes <name>user1</name> to <name>user2</name> . If you figure out how it works plus read SAX

How to get element's value from XML using SAX parser in startElement?

只愿长相守 提交于 2019-11-28 00:46:50
问题 Is it possible to get the content of an element from a XML file in startElement function that is the override function of the SAX handler? Below is the specification. 1) XML file <employees> <employee id="111"> <firstName>Rakesh</firstName> <lastName>Mishra</lastName> <location>Bangalore</location> </employee> <employee id="112"> <firstName>John</firstName> <lastName>Davis</lastName> <location>Chennai</location> </employee> <employee id="113"> <firstName>Rajesh</firstName> <lastName>Sharma<

Java SAX Parsing

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 22:55:32
There's an XML stream which I need to parse. Since I only need to do it once and build my java objects, SAX looks like the natural choice. I'm extending DefaultHandler and implementing the startElement, endElement and characters methods, having members in my class where I save the current read value (taken in the characters method). I have no problem doing what I need, but my code got quite complex and I'm sure there's no reason for that and that I can do things differently. The structure of my XML is something like this: <players> <player> <id></id> <name></name> <teams total="2"> <team> <id>

What ever happened to XPathReader

主宰稳场 提交于 2019-11-27 22:46:43
XPathReader is/ was an implementation of a forward reading XML parser (built on XMLReader) which allowed you to register XPath queries for it to find (or at least a subset of XPath called Sequential XPath ). This seems to be the perfect choice for easy access to elements of xml streams, or case where you just need to pull some information out of the start of a large xml document and therefore don't want to load the whole thing into memory. There seemed to be a flurry of excitement about the open source implementation that one of the MS guys was releasing back in 2003/ 2004, eg: http://donxml

Java XML Parsing: Avoid entity reference resolution

为君一笑 提交于 2019-11-27 21:45:06
I am currently parsing XHTML documents with a DOM parser, like: final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); final DocumentBuilder db = dbf.newDocumentBuilder(); db.setEntityResolver(MY_ENTITY_RESOLVER); db.setErrorHandler(MY_ERROR_HANDLER); ... final Document doc = db.parse(inputSource); And my problem is that when my document contains an entity reference like, for example: <p>€</p> My parser creates a Text node for that content containing "€" instead of "€". This is, it is resolving the entity in the way it is supposed to do it (the XHTML

Java: How to display an XML file in a JTree

心不动则不痛 提交于 2019-11-27 21:42:08
I would like to have a way to display the contents of an XML file in a JTree . I have already accomplished this using DOM, by implementing a custom TreeModel (and TreeCellRenderer ). However it is very clunky (much workaround-ery and hackery) and rather rough around the edges. Is anyone aware of a way to get a JTree to display the contents of an XML file, parsed with SAX? Thanks! Here's the code that I use. It is based on the API of Dom4J, but you can easily convert it to the APIs of your favorite XML library: public JTree build(String pathToXml) throws Exception { SAXReader reader = new

Howto let the SAX parser determine the encoding from the xml declaration?

浪子不回头ぞ 提交于 2019-11-27 19:22:29
I'm trying to parse xml files from different sources (over which I have little control). Most of the them are encoded in UTF-8 and don't cause any problems using the following snippet: SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); FeedHandler handler = new FeedHandler(); InputSource is = new InputSource(getInputStream()); parser.parse(is, handler); Since SAX defaults to UTF-8 this is fine. However some of the documents declare: <?xml version="1.0" encoding="ISO-8859-1"?> Even though ISO-8859-1 is declared SAX still defaults to UTF-8. Only

Efficient XSLT pipeline in Java (or redirecting Results to Sources)

纵饮孤独 提交于 2019-11-27 18:49:59
I have a series of XSL 2.0 stylesheets that feed into each other, i.e. the output of stylesheet A feeds B feeds C. What is the most efficient way of doing this? The question rephrased is: how can one efficiently route the output of one transformation into another. Here's my first attempt: @Override public void transform(Source data, Result out) throws TransformerException{ for(Transformer autobot : autobots){ if(autobots.indexOf(autobot) != (autobots.size()-1)){ log.debug("Transforming prelim stylesheet..."); data = transform(autobot,data); }else{ log.debug("Transforming final stylesheet...");