Giter VIP home page Giter VIP logo

xmltool's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xmltool's Issues

Compile with JDK 1.6

Ah I have another issue. When I use the new jar I get this error: Caused
by: java.lang.UnsupportedClassVersionError: com/mycila/xmltool/XMLDoc :
Unsupported major.minor version 51.0

I think that means it was compiled and targeted for jdk 1.7. I think 4.0
was targeted for jdk1.6. We're using jdk1.6, is there any way you could
make it targeted for 1.6 still?

Thanks,
Dan

Provide a virtual node structure for unavailable nodes.

Reported by christopher.klewes, Apr 20, 2011
First I would like to say thanks for your framework. It's really nice and I'm loving to create XML documents with it.

I recently wanted to read from a XML file which has many optional elements and therefore can occure very differing. I stumbled upon #gotoChild(String xPath) which enables the traversal to a specific child. This is really great but unfortunately it throws an Exception if this child doesn't exists. So I found the #hasTag(String xPath) method which can check this. This works great but bloats up my code extremely.

For example I have the following XML document with the root "user" and the element "name" but it also could optionally contain "login" and "password".

  <user>
    <name>moo</name>
  </foo>

To check this the code would look like

  if(user.hasTag("name")) 
  {
     String name = user.gotoChild("name").getText()
  }
  if(user.hasTag("login")) 
  {
     String login = user.gotoChild("login").getText()
  }

[...]
... and so on!

I would like to have something like Groovys XML slurper. It provides unavaiable nodes/tags if you try to traverse to structured which aren't available.

The code could look like this:

String name = user.gotoChild("name").getText()

Instead of throwing a exception here you could collect the state and return an unavailable tag which returns null on the .getText() call. Also if we try to do something like this:

XMLTag address = user.gotoChild("address")

String street = address.gotoChild("street").getText()

Could also work if you provide a unavailable tag which results null in case of unfound structures.

What do you think?

Hope to hear from you!

Best Regards
Chris
Jun 14, 2011 Delete comment Project Member #1 mathieu.carbou
Yes, I also think that having a null object is a very good idea !!

Use pom 2

  • property to generate other urls and so on
  • New license plugin
  • Fix SCM
  • Maven reports / site
  • Issue tracking
  • Ci Management

Comments ordering

I've made recently a parser using Java's DOM parser that loads up an xmls and correcting some elements, attributes, etc.. The problem is the output format doesnt keeps the comments the way i'd like them to be.

Here's whats happening:

@@ -2107,7 +2121,8 @@
        <set name="enchant_enabled" val="1" />
        <set name="is_freightable" val="false" />
        <skills>
-           <skill id="3599" level="1" /> <!-- Polearm Multi-attack -->
+           <skill id="3599" level="1" />
+           <!-- Polearm Multi-attack -->
        </skills>

I couldn't find a way to keep comments on the right side of the element. I also wanted to create new comments and i could put them either in before the element or after but not right next to it.

is there a way i could preserve such order with your xml tool?

Thanks in advice!

Support encoding (Handling the "Content not allowed in prolog" message)

Reported by austenjt, Jul 24, 2012
When loading a improperly encoded XML file that IS properly formatted XML ( in my case a UTF-8 file), sometimes the XMLDoc.from() method throws the "Content not allowed in prolog" message and I am dead in the water and can no longer use your tool to load the file. In that case I have to write my own 15 lines of code to manually load the file otherwise.

Is it possible you can provide some functionality to help users of your tool get around this message?
Jul 24, 2012 Delete comment #1 austenjt
This was my workaround. I wish I didn't have to do this but since xmltool couldn't read the file , I was required to do this :

    public String getValByXpath( String xpathstr ) {
        // can't use XMLDoc here because XmlInvoiceClient stores invalidly encoded xml
        // so, I do this all manually below just to get the Assert
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
        DocumentBuilder builder;
        XPathExpression xp;
        Document doc;
        String success = null;
        try {
            builder = factory.newDocumentBuilder();
        doc = builder.parse( "result.xml" ); 
        XPathFactory xPathfactory = XPathFactory.newInstance(); 
        XPath xpath = xPathfactory.newXPath();
        xp = xpath.compile( xpathstr );
        success = xp.evaluate( doc ); 
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return success;
    }

Jul 27, 2012 Delete comment #2 austenjt
So, I decided to abandon xmltool in favor of these 2 xml classes I created, which have no problems with reading XML BOM from any file encoding :
http://thegreenoak.blogspot.com/2012/07/writing-string-value-to-xml-file-using.html
Dec 17, 2012 Delete comment Project Member #3 mathieu.carbou
Are you sure the error message is really from XMLTool and not from the Java XML API ? I already had this message: it triggers when your XML is invalid. Usually when you have a special unreadable character at the beginning of the file.

Perhaps because if the input stream of platform encoding ?

Dec 17, 2012 Delete comment #4 austenjt
Yes, I am sure. I provided a code snippet in my July 27th comment that shows that a pure Java XML API works. Sorry, I didn't provide an example file but if you created unit tests with the variations you can think of, you would probably find it, IMHO.

Doctype support

Reported by mikenereson, Sep 12, 2011
Is it possible to set the doctype?

I have an english properties file that I translate to several other languages. It seems in order for Properties#loadFromXML to work I need to include a doctype:

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

Properties#loadFromXML

http://download.oracle.com/javase/1,5.0/docs/api/java/util/Properties.html#loadFromXML(java.io.InputStream)
Sep 12, 2011 Delete comment #1 mikenereson
Not your problem, not information nontheless, here is the stacktrace from trying to load the file created with XML tool

Exception in thread "main" java.util.InvalidPropertiesFormatException: org.xml.sax.SAXParseException: Document root element "properties", must match DOCTYPE root "null".
    at java.util.XMLUtils.load(XMLUtils.java:59)
    at java.util.Properties.loadFromXML(Properties.java:852)
    at com.retrieve.XmlApp.main(XmlApp.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.xml.sax.SAXParseException: Document root element "properties", must match DOCTYPE root "null".

And here is a sample of the file being loaded.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
    <entry key="i18n.language.abbreviation">pl</entry>
    <entry key="i18n.language.name">Polish</entry>
    <entry key="application.header.home">Dom</entry>

Sep 12, 2011 Delete comment #2 mikenereson
My solution, for the record, while waiting to hear if this functionality is available in XML tool, was to write the XML file out, then read it back in add the doctype and write it out again.

http://www.exampledepot.com/egs/javax.xml.transform/SetDocType.html

    private static void addPropertiesDoctype(final File xmlFile)
    {
        final Document document = BasicDom.parseXmlFile(xmlFile.getAbsolutePath(), false);

        try
        {
            final Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://java.sun.com/dtd/properties.dtd");

            final Source source = new DOMSource(document);
            final Result result = new StreamResult(xmlFile);
            transformer.transform(source, result);
        }
        catch (final TransformerConfigurationException e)
        {
        }
        catch (final TransformerException e)
        {
        }
    }

resulting in

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="i18n.language.abbreviation">pl</entry>
    <entry key="i18n.language.name">Polish</entry>
    <entry key="application.header.home">Dom</entry>

Sep 12, 2011 Delete comment Project Member #3 mathieu.carbou
Hi,

Thanks for the report, this is definitely a feature that should be added. I'll try to do my best to spend time on it, plus other requests, by the end of the month and release a new version. I am very busy at the moment.

Status: Accepted
Owner: mathieu.carbou
Labels: Type-Enhancement Priority-High Milestone-Release3.4
Sep 12, 2011 Delete comment #4 mikenereson
Mathieu, don't rush for me. I have a solution that works. I really just wasn't sure if I was not looking in the right place, or if it was a missing feature.

I thought I could get at the underlying dom builder, or something, to set the value, but if its a missing feature, then you've confirmed that its not at this time possible to do this.

Thanks, for the tool. Been using it on several projects since 2008.

Exception thrown from XMLDocumentException constructor

java.util.MissingFormatArgumentException: Format specifier '%s'
    at java.util.Formatter.format(Formatter.java:2519)
    at java.util.Formatter.format(Formatter.java:2455)
    at java.lang.String.format(String.java:2927)
    at com.mycila.xmltool.XMLDocumentException.<init>(XMLDocumentException.java:23)
    at com.mycila.xmltool.XMLDocPath.findNode(XMLDocPath.java:90)
    at com.mycila.xmltool.XMLDoc.gotoTag(XMLDoc.java:478)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at com.mycila.xmltool.XMLDocBuilder$1.invoke(XMLDocBuilder.java:95)

this happens when you invoke p.gotoTag("some[xpath = %s]", param) and a target node is not found

XMLTag creation is not thread safe

We use xmltool in an environment where a lot of different threads create and modify small xml documents, and we have noticed that the XMLDoc.from(...) are not thred-safe. This is implicitly documented in XMLTag where it clearly states that it is not thread-safe, but it was a surprise to us anyway that the creation of unrelated XMLTags are not possible in a multi-thread environment without proper synchronization.

As far as I can tell, the root problem is that XMLDocumentBuilderFactory caches and reuses its DocumentBuilders, and these builders are not thread-safe. We would therefor need an option to use creation that does not reuse DocumentBuilders.

Again, I can see why the use of a created XMLTag is not thread-safe, but it should be possible to create different XMLTags in different threads at the same time.

Insert before

Reported by nzjess, Apr 6, 2011
If I have this document:

And I want to edit it using XML Tool to get this document:

What are the calls I should use?

My problem is that using addTag() I can only get this:

What have I missed?

Thanks,
Jesse.

Jun 14, 2011 Delete comment Project Member #1 mathieu.carbou
Hi,

Yes I fear this use case (insert before) does not exist yet. addChild 'appends'.

=> feature to implement !

document.normalize in multi-thread env has performance problem

Reported by hxliang1982, Jun 28, 2011
when doing normalize, DOMConfigurationImpl will be created if hasn't been, while when constructing DOMConfigurationImpl, DTDDVFactory.getInstance will be invoked and this method is globally synchronized.

So, when we were testing in multi-thread env, quite many threads are locked at that "getInstance" method.

My question is, is it possible to add a flag to disable normalize?

Thanks.
Jun 28, 2011 Delete comment #1 hxliang1982
BTW, seems that this lock only exists in JDK built-in xerces.
Aug 21, 2011 Delete comment Project Member #2 mathieu.carbou
Hi,

Thanks for the report. I'll check when I'll have some time.
Status: Accepted
Owner: mathieu.carbou
Labels: Type-Enhancement Priority-Low
Aug 21, 2011 Delete comment #3 hxliang1982
Thanks for you reply. And btw, is it possible not to create an xpath instance when building XMLDocDefinition since create an xpath instance is time consuming in multi-thread env because of class loader lock.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.