• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding a new Site under a Sites in XML and using SAXBuilder builder = new SAXBuilder();

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Im a little furstarated :-( ..

I want to add some elementtags and value in a xml.
I have alvays only added "single elements" or totaly new tags..
now I have a xml that look like this:

and now I want to add 2 new site
first:
Name = funweb;
popular = soon;
then later:
Name = sadweb;
popular = no;
..
but i only can rename it..

i use this so far.. :


but the result is not adding is only take the fist value and change this..
(the function I have sending 2 strings and the XML in stringformat I then want to return the new xml in a string format...)
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So lines 12 to 18 of that code add a <site> element to your document. If you want to add two <site> elements then you have to execute that code twice, with the appropriate data in lines 15 and 18 where you pass something to the setText() method. I would recommend a method which does just that and nothing else. Then call that method twice with the appropriate parameters.
 
Niklas Karlsson
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So just to explain it:
In the code I
have a xml code that I read in a string every time I want to add something in the xml file.
so:
...Some code.... and then I call:
AddSitesToXmlstring(xmlString, name1, popular1)
// this should add in sites a new site/name1 and site/popular...
...Some code.... and then I call:
AddSitesToXmlstring(xmlString, name2, popular2)
// this should add in sites another new site/name1 and site/popular...

But I "think" I have written a code that only add a site in sites, but then just "forget" this and go to the first site it finds and add/over-write the name and popular, :-(

I hope you understand the problem better..
 
Niklas Karlsson
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So for example :
....
...Some code.... and then:
String name1,name2,popular1,popular2;
...
...Some code.... and then:
name1 = "funweb";
Popular1 = "soon";
...Some code.... and then I call:
AddSitesToXmlstring(xmlString, name1, popular1)
// this should add in sites a new site/name1 and site/popular...
...
...Some code.... and then:
name2 = "sadweb";
popular2 = "no";
...Some code.... and then I call:
AddSitesToXmlstring(xmlString, name2, popular2)
// this should add in sites another new site/name1 and site/popular...
....
---
Public string AddSitesToXmlstring(string downloadedtemp, String name, String popular){
String xml = downloadedtemp;
SAXBuilder builder = new SAXBuilder();
try {
Document document = builder.build(
new ByteArrayInputStream(xml.getBytes()));

document.getRootElement().getChild("sites").addContent(new Element("site"));

document.getRootElement().getChild("sites").getChild("site").addContent(new Element("Name"));
document.getRootElement().getChild("sites").getChild("site").getChild("Name").setText(name);

document.getRootElement().getChild("sites").getChild("site").addContent(new Element("Popular"));
document.getRootElement().getChild("sites").getChild("site").getChild("Name").setText(popular);

XMLOutputter outputter =
new XMLOutputter(Format.getPrettyFormat());
outputter.output(document, System.out);
outputter.output(document, new FileWriter("filetemp.xml"));
xml = readFile("filetemp.xml");
System.out.println("----test if it looks ok: ----");
System.out.println(xml);
downloadedtemp = xml;
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return downloadedtemp;
}


--------

BUT THE RESULT IS :
<result>
<sites>
<site>
<Name>sadweb</Name>
<popular>no<popular>
<\site>

<site>
<Name> website1</Name>
<popular>no<popular>
<\site>

<\site>

<\site>

</sites>
</result>

And not what I wanted :-(
 
Niklas Karlsson
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or can I add it to "last child " or something is this better ?
 
Niklas Karlsson
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find a solution that works:
"
Element sites = document.getRootElement().getChild("sites");
Element site = new Element("site");

Element siteE = new Element("Name").setText(nameString);
Element popE= new Element("popular").setText(popularString);

site.addContent(siteE);
site.addContent(popE);

sites.addContent(site);
"

:-)
 
Niklas Karlsson
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BUT then i insert site:
SpecialåäöÅÄÖáé
it save "rubbisch" in the xml :-(
( like: SpecialåäöÃ...Ã"Ã-áé )

So i did not make ti 100%

or is is a simple "nameS.replaceAll("Æ" , "& # 1 9 8 ; ");" or something???

:-(
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic