2

I have an XML file as follows

<abc>
  <property>
    <Recentlyopenedfiles>
    <File Path="c:\hai.txt" />
    <File Path="C:\old.java" />
    </Recentlyopenedfiles>
   </property>
</abc>

I am using jdom2 SAXBuilder to parse the xml in java.I need to save all recently opened files in <Recentlyopenedfiles>. At the maximum this has to hold only three paths(i.e) 3 recently opened files path.

So,I need to know

  1. Are there any mechanism to set limit to child nodes?
  2. How to index child and perform stack operation so that last opened file saved in first file path.

Thanks.

rolfl
  • 17,539
  • 7
  • 42
  • 76
user3131471
  • 71
  • 1
  • 6

4 Answers4

4

Jon's answer is closest to what I would recommend. The mecahinics of it are easier than it sounds though:

Document doc = saxbuilder.build(....);
Element abc = doc.getRootElement();
Element recentelement = abc.getChild("properties").getChild("Recentlyopenedfiles");
List<Element> recentfiles = recentelement.getChildren("File");
recentfiles.add(0, new Element("File").setAttribute("Path", filepath));
while (recentfiles.size() > 3) {
   recentfiles.remove(3);
}
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
xout.output(doc, outputstream);
rolfl
  • 17,539
  • 7
  • 42
  • 76
2

There are declarations in XSD to limit the number of child elements, but I don't think that's relevant in your particular case. When it comes to parsing and then modifying the XML, you'll just need to perform the limiting by hand.

In terms of "stacking" - if you just always add items to the start of the element rather than at the end (using parent.addContent(0, newChild)) , you'll end up with the last one you add being the first in the document. You can then count how many you've got, and remove any extra ones.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0
  1. You can limit number of child nodes by using XSD schema and validate your XML file against it. Here you can find how to set max number of nodes.
  2. You can use DOM parser. You can iterate through all nodes in XML on the same level and count number of "File" nodes.
  3. If you need to parser HUGE XML file, DOM parser may be not appropriate. In this case StAX parser can be a better choice. Just count number of times "File" open tag occurred and handle a case if it occurs more than 3 times.
Community
  • 1
  • 1
Ivan Mushketyk
  • 8,107
  • 7
  • 50
  • 67
0

Setting limit to the child nodes. Assuming that you do not create the XMLs that you parse, you can define the number of child elements in schema (XSD)

Something like:

<xs:element name="pets">
  <xs:complexType>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="dog" type="xs:string"/>
      <xs:element name="cat" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

If you then add the elements from the code then just add them always on top: Use addContent with index=0 as described here

elem.addContent(0, newChild);
aviad
  • 8,229
  • 9
  • 50
  • 98