dom4j

Reading a single XML document from a stream using dom4j

I'm trying to read a single XML document from stream at a time using dom4j, process it, then proceed to the next document on the stream. Unfortunately, dom4j's SAXReader (using JAXP under the covers) keeps reading and chokes on the following document element. Is there a way to get the SAXReader to stop reading the stream once it finds ...

Need help making Jython (dom4j) script more graceful :)

Hi All, I have started using Jython as it seems to be a excellent language, and has proved to be so far. I am using dom4j to manipulate and retrieve data from the DOM of a bunch of HTML files I have on disk. I have wrote the below script to check threw the DOM using Xpath for H1 tags and grab text, if a H1 tag is not present in the DO...

Is adding elements between elements with Jython and dom4j possible?

Hi, Just playing with dom4j, excellent. I was a PHP developer for a year and half, just got a new job and started playing around with Jython now I have spare time, its a great language. Was thinking about trying to add a element in between element, example below: <div id="content"> <p>Some text in here</p> <!--New p tag here--...

Need help handling documents using dom4j

import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.XMLWriter; public class Main { public static void main(String[] args){ Company cp17 = new Company(); Person ps1 = new Person("Barry","15900000000"); Person ps2 = new Person("Andy","15...

Is there any value in creating a pool of Dom4J SaxReader objects?

I'm using Dom4J 1.4.2. Right now my code creates a new SaxReader every time I want to parse a new XML document: SAXReader reader = new SAXReader( ); Is there any value in creating a pool of SaxReader objects and just reusing them? How much overhead is involved in creating a new SaxReader on every call? My code could get one from t...

How efficient is XPath compared to using DOM in Dom4J?

For example consider the following xml <root> <childNode attribute1="value1"> <grandChildNode attrib1="val1" attrib2="val2">some content1 </grandChildNode> <grandChildNode attrib1="val1" attrib2="val2">some content2 </grandChildNode> <grandChildNode attrib1="val1" attrib2="val2">some content3 </grandChild...

Dom4j rule does not match all expected nodes

I'm using dom4j's rules api to fire an action if the predefined pattern //authorize matches an element in the following xml snippet. <authorize role_required="admin"> <node title="node which is visible only to admin" path="" > <authorize role_required="admin"> <node title="node which is visible only to admin" path=""/> ...

What Java XML library do you recommend (to replace dom4j)?

I'm looking for something like dom4j, but without dom4j's warts, such as bad or missing documentation and seemingly stalled development status. Background: I've been using and advocating dom4j, but don't feel completely right about it because I know the library is far from optimal (example: see how methods in XSLT related Stylesheet cla...

Dom4j detach node, Jython

Hey, I am using Dom4j to detach a node, like below: <div name="divName"> Some Text Here <span>Some Text Here</span> </div> I am selecting the div node by name and then using the detach method to remove it: xpathValue = "//*[contains(@name, 'divName')]" xpath = dom.createXPath(xpathValue) if xpath != None: nodes =...

Converting document encoding when reading with dom4j

Is there any way I can convert a document being parsed by dom4j's SAXReader from the ISO-8859-2 encoding to UTF-8? I need that to happen while parsing, so that the objects created by dom4j are already Unicode/UTF-8 and running code such as: "some text".equals(node.getText()); returns true. ...

dom4j SAXReader - stop parsing

I am parsing an XML file with SAXReader and would like to stop after a certain amount of time (timeout) in case the file is too large. At a high level, I would like to throw a SAXException in the ContentHandler (in, say, startElement). The idea is described here: http://www.ibm.com/developerworks/xml/library/x-tipsaxstop/ However, dom4...

dom4j converting text to special characters

I'm using dom4j to parse my xml. Let's say I have something like this: <?xml version="1.0" encoding="UTF-8"?> <foo> <bar>&#402;</bar> </foo> When looking at the value of the "bar" node, it gives me back the special character as represented by "& #402;" Is there a way to prevent this and just read in the actual bit of text? ...

Clean namespace handling with dom4j

Hi, We are using dom4j 1.6.1, to parse XML comming from somewhere. Sometime, the balise have mention of the namespace ( eg : ) and sometime not ( ). And it's make call of Element.selectSingleNode(String s ) fails. For now we have 3 solutions, and we are not happy with them 1 - Remove all namespace occurence before doing anything wi...

Using Xpath in Dom4j

I get the following exception when trying to access any nodes of a parsed xml document on dom4j: Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230) at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207) at org.dom4j.tree.Abstr...

remove element in dom4j

<root> <elm id="1"/> <elm id="2"/> <elm id="3"/> <elm id="4"/> </root> I want to leave id="2" in the dom, how can domj4 to remove the other three ? result: <root> <elm id="2"/> </root> ...

dom4j cdata (keep whitespace)

Hello, I'm trying to get cdata text from a node using dom4j java. My issue here is that all of the line breaks are removed. Essentially, I need to read the contents of the CDATA as if it were a <pre> tag in HTML. Do you have any ideas? I have a very small time to get this done unfortunately so any help would be appreciated. Tha...

SAXReader not re-ecape characters

I'm reading a XML file with dom4j. The file looks like this: ... <Field>&#13;&#10; hello, world...</Field> ... I read the file with SAXReader into a Document. When I use getText() on a the node I obtain the followin String: \r\n hello, world... I do some processing and then write another file using asXml(). But the characters are ...

Write dom4j Document to xml-file with escaped '\r\n\'

Sorry for asking about quite the same issue, but now i would like to: write a dom4j Document which contains tags looking like this: <Field>\r\n some text</Field> to a xml file, but the \r\n should be escaped to &#13;&#10; org.dom4j.Document.asXml() does not work. ...

Mapping complex result set with Dom4j

I'm trying to create an XML document from a complex result set, i.e. a result set with multiple joins, so a competition has a bunch of questions, and a question has a bunch of answers. So the document would end up looking something like: <competitions> <competition id="12"> <question id="3"> <answer id="34"> The ans...

HttpServletResponse XML to Java

I am maintaining this servlet that has a HttpServletResponse response that replies back to the client an XML message. I want to take the XML message and convert it to JSON, then send the JSON back. I want to avoid writing my own JSON converter if possible. Does anyone have a good method of doing this? I googled for this: http://pvoss.w...