JTree node not getting added as expected - java

I am trying to Parse the below XML file and create an JTree based on it.
<OBJECTS>
<WINDOW NAME = "WINDOW 01" URL = "URL 01">
<PAGE NAME = "PAGE 01" URL = "PAGE URL 01">
</PAGE>
</WINDOW>
</OBJECTS>
The "WINDOW 01" is getting added to the root node "Object List", but the "PAGE 01" node is not getting displayed under the "WINDOW 01" node. The source code used is given below. Please Help!!!
public class DataNode extends DefaultMutableTreeNode {
private static final long serialVersionUID = 1L;
public String ObjectType, ObjectName, URL, ElementType;
public DefaultMutableTreeNode node;
public DataNode(DefaultMutableTreeNode node, Element element) {
this.node = node;
this.ObjectType = element.getTagName();
this.ObjectName = element.getAttribute("NAME");
this.URL = element.getAttribute("URL");
this.ElementType = element.getAttribute("TYPE");
}
public DataNode(Element element) {
this.node = new DefaultMutableTreeNode("OBJECT");
this.ObjectType = element.getTagName();
this.ObjectName = "Object List";
this.URL = "";
this.ElementType = "";
}
#Override
public String toString() {
return this.ObjectName;
}
}
This is the main class. No errors or warnings are displayed on compiling.
public class MyOwn {
private JFrame contentsFrame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyOwn window = new MyOwn();
window.contentsFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyOwn() {
contentsFrame = new JFrame();
contentsFrame.setTitle("My JTree");
contentsFrame.setBounds(100, 100, 549, 738);
contentsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree objectListTree = new JTree(convertXMLtoTree("G:/Collection.xml"));
objectListTree.setAlignmentY(Component.TOP_ALIGNMENT);
objectListTree.setAlignmentX(Component.LEFT_ALIGNMENT);
contentsFrame.getContentPane().add(new JScrollPane(objectListTree));
}
private DefaultMutableTreeNode convertXMLtoTree(String Path) {
NodeList nWindow, nPage;
DefaultMutableTreeNode dRoot, dWindow, dPage;
DataNode xRoot, xWindow, xPage;
try {
File fXmlFile = new File(Path);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
xRoot = new DataNode(doc.getDocumentElement());
dRoot = new DefaultMutableTreeNode(xRoot);
nWindow = doc.getDocumentElement().getElementsByTagName("WINDOW");
for (int i = 0; i < nWindow.getLength(); i++) {
dWindow = new DefaultMutableTreeNode(((Element)(nWindow.item(i))).getAttribute("NAME"));
xWindow = new DataNode(dWindow, (Element)(nWindow.item(i)));
dRoot.add(xWindow);
nPage = ((Element)(nWindow.item(i))).getElementsByTagName("PAGE");
for (int j = 0; j < nPage.getLength(); j++) {
dPage = new DefaultMutableTreeNode(((Element)(nPage.item(j))).getAttribute("NAME"));
xPage = new DataNode(dPage, (Element)(nPage.item(j)));
dWindow.add(xPage);
}
}
return dRoot;
} catch (Exception e) {
return null;
}
}
}

Add your custom DefaultMutableTreeNode DataNode to child node for WINDOW 01 rather than the dWindow which never gets added to the JTree. Replace
dWindow.add(xPage);
with
xWindow.add(xPage);

Related

Most recent version of XML file not being read

I have created an application which takes user input from textfields and stores it in an XML file. The user can then reload these inputs back into a text field on the click of a button.
The issue I'm having is that the most recent version of the XML file is not being used - for example when I start the application and save new inputs the load function doesn't load the input even though the XML file has the data in it. When I close and then restart the application the load functionality works using the data from the last session. If I try adding new inputs the load button will just cycle through the data from the last session, not using the new points.
So my question is why isn't the current state of the XML file being read?
Save to XML Code:
ArrayList<SavedPosition> posList = new ArrayList<SavedPosition>();
String SAVEDPOS_XML = "savedPos.xml";
save.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
SavedPosition saved = new SavedPosition();
saved.setPosition1(mediaPlayerComponent.getMediaPlayer().getTime());
saved.setPosition2(mediaPlayerComponent2.getMediaPlayer().getTime());
saved.setNBehaviour(textArea.getText());
saved.setABehaviour(textArea1.getText());
saved.setGap(textArea2.getText());
saved.setForces(textArea3.getText());
saved.setFindings(textArea2.getText());
posList.add(saved);
SavedPositions savedPos = new SavedPositions();
savedPos.setSavedPositions(posList);
try {
// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(SavedPositions.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//print
m.marshal(savedPos, System.out);
//write to file
m.marshal(savedPos, new File(SAVEDPOS_XML));
} catch (JAXBException ex) {
Logger.getLogger(DoubleViewer.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
XML to Object code:
public class XmlToObject {
public static ArrayList<ArrayList<String>> main() {
ArrayList<ArrayList<String>> obj1 = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> obj = new ArrayList<ArrayList<String>>();
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("savedPos.xml");
XPath xp = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xp.compile("//savedPosition").evaluate(d, XPathConstants.NODESET);
System.out.println("number of saved positions " + nl.getLength());
for (int i = 0; i < nl.getLength(); i++){
String a = (xp.compile("./NBehaviour").evaluate(nl.item(i)));
String b = (xp.compile("./ABehaviour").evaluate(nl.item(i)));
String c = (xp.compile("./gap").evaluate(nl.item(i)));
String d1 = (xp.compile("./forces").evaluate(nl.item(i)));
String e = (xp.compile("./findings").evaluate(nl.item(i)));
String f = (xp.compile("./position1").evaluate(nl.item(i)));
String g = (xp.compile("./position2").evaluate(nl.item(i)));
ArrayList<String> savedPosition = new ArrayList<String>();
savedPosition.add(a);
savedPosition.add(b);
savedPosition.add(c);
savedPosition.add(d1);
savedPosition.add(e);
savedPosition.add(f);
savedPosition.add(g);
obj.add(savedPosition);
}
return obj;
}catch(Exception l){
System.out.println(l.getMessage());
}
return obj1;
}
}
Load button code:
List<ArrayList<String>> obj = XmlToObject.main();
int a = obj.size();
load.addActionListener(new ActionListener() {
int displayedPositionIndex = 0;
public void actionPerformed(ActionEvent e) {
displayedPositionIndex++;
if(displayedPositionIndex >= obj.size()) {
displayedPositionIndex = 0; // to loop back to first after last position
}
setPosition(displayedPositionIndex);
}
private void setPosition(int index) {
List<String> positionData = obj.get(index);
textArea.setText(positionData.get(0));
textArea1.setText(positionData.get(1));
textArea2.setText(positionData.get(2));
textArea3.setText(positionData.get(3));
textArea4.setText(positionData.get(4));
Long position = Long.valueOf(positionData.get(5));
mediaPlayerComponent.getMediaPlayer().setTime(position);
mediaPlayerComponent2.getMediaPlayer().setPosition(Float.parseFloat(positionData.get(6)));
}
});

Can not populate List<Item> from XML nodes

I have an xml document to parse which have nested nodes I have tried in my way but unable to get job done as desired.
XML doc is
<Items>
<Item>
<MediumImage>
<URL>http://ecx.images-amazon.com/images/I/51l7DDD1qNL._SL160_.jpg</URL>
<Height Units="pixels">160</Height>
<Width Units="pixels">160</Width>
</MediumImage>
<Title>Fallout 4 Vault Dweller's Survival Guide Collector's Edition: Prima Official Game Guide</Title>
<OfferSummary>
<LowestNewPrice>
<Amount>1952</Amount>
</OfferSummary>
</Item>
.
.
.
</Items>
I have done to fetch Title node values as
private static NodeList fetchTitle(String requestUrl) {
NodeList nodeList = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(requestUrl);
nodeList = doc.getElementsByTagName("Title");
} catch (Exception e) {
System.out.println("Message is " + e.getCause() + "...." + e.getMessage());
throw new RuntimeException(e);
}
return nodeList;
}
and I print as in main() as
titleList = fetchTitle(requestUrl);
for (int i = 0; i < titleList.getLength(); i++) {
Node node = titleList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
// do something with the current element
System.out.println(node.getNodeName());
System.out.println("Signed Title is \"" + node.getTextContent() + "\"");
System.out.println();
}
}
and Amount value from LowestNewPrice Node as
private static NodeList fetchPrice(String requestUrl) {
NodeList nodeList = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(requestUrl);
nodeList = doc.getElementsByTagName("LowestNewPrice");
} catch (Exception e) {
System.out.println("Message is " + e.getCause() + "...." + e.getMessage());
throw new RuntimeException(e);
}
return nodeList;
}
and I print as in main() as
priceList = fetchPrice(requestUrl);
for (int i = 0; i < priceList.getLength(); i++) {
Node node = priceList.item(i).getFirstChild();
if (node.getNodeType() == Node.ELEMENT_NODE) {
// do something with the current element
System.out.println(node.getNodeName());
System.out.println("Signed Price is \"" + node.getTextContent() + "\"");
System.out.println();
}
}
through above code I get all Title values first and then I get Amount values separately, but what I really want is to have a POJO class as
public class Item {
String title;
String price;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
and add values to Item object using setTitle(), setPrice(), and return a List<Item>
any help please.
You might try this solution please.
Parse your data and add to List<Item> as :
public static Document fetchRequiredData(String src) {
Document doc = null;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser;
try {
parser = dbFactory.newDocumentBuilder();
doc= parser.parse(src);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return doc;
}
public static List<Item> parseItemInfo(Document doc){
List<Item> items = new ArrayList<Item>();
NodeList itemNodeList = doc.getElementsByTagName("Item");
for (int i = 0; i < itemNodeList.getLength(); i++) {
Node titleNode = doc.getElementsByTagName("Title").item(i);
Node priceNode = doc.getElementsByTagName("LowestNewPrice").item(i).getFirstChild();
if (titleNode.getNodeType() == Node.ELEMENT_NODE || priceNode.getNodeType() == Node.ELEMENT_NODE ) {
Item item = new Item();
item.setDesc(titleNode.getTextContent());
item.setPrice(priceNode.getTextContent());
items.add(item);
}
}
return items;
}
now your List is ready to test in main() method as
public static void main(String[] args) {
List<Item> items = parseItemInfo(fetchRequiredData(requestUrl));
System.out.println("Printing List<Item> contents ...");
for (Item item : items) {
System.out.println("Title is " + item.getTitle());
System.out.println("Price is " + item.getPrice());
System.out.println();
}
}
Hope this one helps.
You can achieve that by using JAXBContext. First create Item class.
#XmlRootElement(name = "Items")
#XmlAccessorType(value = XmlAccessType.FIELD)
public class Items
{
#XmlElement(name = "Item")
private List<Item> item;
public void setItem(List<Item> itemList)
{
this.item = itemList;
}
public List<Item> getItem()
{
return this.item;
}
}
#XmlRootElement(name = "Item")
#XmlAccessorType(vallue = XmlAccessType.FIELD)
public class Item
{
#XmlElement(name = "MediumImage")
private MediumImage image;
#XmlElement(name = "Title")
private String title;
#XmlElement(name = "OfferSummary")
private OfferSummary summary;
getters();
setters();
}
#XmlRootElement(name = "MediumImage")
#XmlAccessorType(value = XmlAccessType.FIELD)
public class MediumImage
{
#XmlElement(name = "URL")
private String url;
....
}
#XmlRootElement(name = "OfferSummary")
#XmlAccessorType(value = XmlAccessType.FIELD)
public class OfferSummary
{
#XmlElement(name = "LowestNewPrice")
private LowestNewPrice lowestPrice;
....
}
Then from the main method use marshaller and unmarshaller method.
public static void main(String[] args)
{
File xmlFile = new File("file path");
JAXBContext context = JAXBContext.newInstance(Items.class);
//To get POJO from xml
Unmarshaller unmarshaller = context.createUnmarshaller();
Items items = (Items) unmarshaller.unmarshal(xmlFile);
}
Currently it seems that you're separating the prices and titles into 2 lists, if you want to store an item's price and title into a single Item object, you can do something like this:
public class Item {
public static void main(String[] args) {
ArrayList<Item> items = new ArrayList<Item>();
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("items.xml"));
NodeList itemElements = doc.getElementsByTagName("Item");
for (int i = 0; i < itemElements.getLength(); i++) {
Node itemElement = itemElements.item(i);
NodeList itemChildren = itemElement.getChildNodes();
Item item = new Item();
for (int j = 0; j < itemChildren.getLength(); j++) {
Node n = itemChildren.item(j);
if (n.getNodeName().equalsIgnoreCase("title")) {
item.setTitle(n.getTextContent());
} else if (n.getNodeName().equalsIgnoreCase("OfferSummary")) {
NodeList offerChildren = n.getChildNodes();
for (int k = 0; k < offerChildren.getLength(); k++) {
Node offerChild = offerChildren.item(k);
if (offerChild.getNodeName().equalsIgnoreCase("LowestNewPrice")) {
item.setPrice(offerChild.getTextContent());
}
}
}
}
items.add(item);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("items: " + items);
}
String title;
String price;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
#Override
public String toString() {
return "Title: " + title + " Price: " + price;
}
}
What this does is to get all the <Item> elements from the XML and loop through them to get the item's title and price.

Do I need to create a DocumentBuilderFactory everytime?

I need to update the news feeds for every 5 minutes from a RSS feed .
I have written a TimerTask as shown below
public class TimerTaskForAllNews
{
public static void main( String[] args )
{
TimerTask task = new AllNewsUpdatrUtility();
Timer timer = new Timer();
timer.schedule(task, 1000,60000);
}
}
This is my TimerTask implementation class
package com.util;
import java.net.URL;
public class AllNewsUpdatrUtility extends TimerTask {
private static AllNewsUpdatrUtility instance = null;
public AllNewsUpdatrUtility() {}
public static AllNewsUpdatrUtility getInstance() {
if (instance == null)
instance = new AllNewsUpdatrUtility();
return instance;
}
#Override
public void run() {
try {
JSONArray latestnews = new JSONArray();
JSONObject jsonobj_allnews = new JSONObject();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL url = new URL("http://www.rssmix.com/u/8160628/rss.xml");
Document doc = builder.parse(url.openStream());
NodeList items = doc.getElementsByTagName("item");
for (int i = 0; i < items.getLength(); i++) {
Element item = (Element) items.item(i);
String title = getValue(item, "title");
String link = getValue(item, "link");
String pub_date = getValue(item, "pubDate");
} // for loop ends here
} catch (Exception e) {
e.printStackTrace();
}
}
}
Could you please let me know i can improve this program anyway ?
The specification JSR 206 Java™ API for XML Processing (JAXP) 1.4 says:
It is expected that the newSAXParser method of a SAXParserFactory implementation, the newDocumentBuilder method of a DocumentBuilderFactory and the newTransformer method of a TransformerFactory will be thread safe without side effects.
As said in the comment you can cache the DocumentBuilderFactory instance :
package com.util;
import java.net.URL;
public class AllNewsUpdatrUtility extends TimerTask {
private static AllNewsUpdatrUtility instance;
private final DocumentBuilderFactory dbf;
private AllNewsUpdatrUtility() {}
public synchronized static AllNewsUpdatrUtility getInstance() {
if (instance == null)
instance = new AllNewsUpdatrUtility();
dbf = DocumentBuilderFactory.newInstance();
return instance;
}
#Override
public void run() {
try {
JSONArray latestnews = new JSONArray();
JSONObject jsonobj_allnews = new JSONObject();
DocumentBuilder builder = dbf.newDocumentBuilder();
URL url = new URL("http://www.rssmix.com/u/8160628/rss.xml");
Document doc = builder.parse(url.openStream());
NodeList items = doc.getElementsByTagName("item");
for (int i = 0; i < items.getLength(); i++) {
Element item = (Element) items.item(i);
String title = getValue(item, "title");
String link = getValue(item, "link");
String pub_date = getValue(item, "pubDate");
} // for loop ends here
} catch (Exception e) {
e.printStackTrace();
}
}
}

Parsing xml with & < > " ' by java DOM parser

Good day ppl.
I have class:
public class XmlModifier {
private DocumentBuilderFactory docFactory = null;
private DocumentBuilder docBuilder = null;
private Document document = null;
private TransformerFactory transformFactory = null;
private Transformer transform = null;
private DOMSource source = null;
private StreamResult streamRes = null;
private boolean exepDocBuilderAlarm = true;
public XmlModifier() {
this.docFactory = DocumentBuilderFactory.newInstance();
try {
this.docBuilder = docFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
this.exepDocBuilderAlarm = false;
e.printStackTrace();
}
}
public void prepareXMLMessage(File file, String userName, String userPassword, String reqId, Integer NUMBER_OF_MSG_SENDS) {
if (exepDocBuilderAlarm != false) {
try {
document = docBuilder.parse(file);
setElementValues("si:sourceInfo", addElementsToArray("si:reqId,si:reqTag"), addElementsToArray(reqId + "," + reqId), NUMBER_OF_MSG_SENDS);
setElementValues("usr:user", addElementsToArray("usr:id,usr:password"), addElementsToArray(userName + "," + userPassword), NUMBER_OF_MSG_SENDS);
setElementValues("meth:method", addElementsToArray("meth:id,meth:tag"), addElementsToArray(reqId + "," + reqId), NUMBER_OF_MSG_SENDS);
setElementValues("tb:tradeField", addElementsToArray("tb:value"), addElementsToArray(reqId), NUMBER_OF_MSG_SENDS);
this.transformFactory = TransformerFactory.newInstance();
this.transform = transformFactory.newTransformer();
this.source = new DOMSource(document);
this.streamRes = new StreamResult(file);
this.transform.transform(source, streamRes);
System.out.println("Done to execute XmlModifier");
} catch (Exception e) {
e.printStackTrace();
} finally {
this.docFactory = null;
this.docBuilder = null;
this.transformFactory = null;
this.transform = null;
this.source = null;
this.streamRes = null;
}
}
}
private void setElementValues(String rootElement, String[] childElements, String[] childElementsValues, Integer msgIDIncrement) {
Node nodeRootElement = document.getElementsByTagName(rootElement).item(0);
NodeList childElementlist = nodeRootElement.getChildNodes();
for (int i = 0; i < childElements.length; i++) {
for (int z = 0; z < childElementlist.getLength(); z++) {
Node node = childElementlist.item(z);
if (childElements[i].equals(node.getNodeName())) {
node.setTextContent(childElementsValues[i]);
}
}
}
}
private String[] addElementsToArray(String elements) {
String[] theArray = null;
theArray = elements.split(",");
return theArray;
}
}
On input I have xml which is not well formed sometimes. The problem of DOM parser is that in my point of view DOM parser parsing whole xml from the beggining which my have special symbols like < > & " ' is not good for me.
How can I on input to prepareXMLMessage(..) provide xml with < > & " ', then in prepareXMLMessage(..) parse it with no problems(change some values inside elements) and than as output provide xml with < > & " ' back???
Thank you for help.

Java DOM XML Parsing

So I have an XML file in the format:
<projectlist>
<project>
<name>test</name>
<type>deploy</type>
<environment>dev</environment>
<server>test01</server>
<server>test02</server>
<server>test03</server>
</project>
</projectlist>
I'm trying to parse this file and build an object that I can populate a JListBox with the names and a radiobutton group with the different servers, however each project consists of a different amount of servers. How do I iterate the nodes/childnodes to build the object with multiple servers. Here is snippets of the code I'm using borrowed from a website and some from me and I'm not very good at coding yet so bear with me please. When I debug it starts to parse & build the object but once it gets to the server names it prints a null pointer exception so I'm doing something totally wrong.
public class XMLParser {
public Project currentProject = new Project();
public void parseXML() throws Exception {
try {
File file = new File("c:\\projectlist.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("project");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
currentProject.SetAppName(getTagValue("name", eElement));
currentProject.SetType(getTagValue("type", eElement));
currentProject.SetEnvironment(getTagValue("environment", eElement));
currentProject.SetServerName(getTagValue("server", eElement));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
public final class Project {
protected String AppName = null;
protected String Type = null;
protected List<String> ServerNames = null;
protected String Environment = null;
public void SetAppName(String AppName) {
this.AppName = AppName;
}
public void SetType(String DeployType) {
this.Type = DeployType;
}
public void SetServerName(String ServerName) {
this.ServerNames.add(ServerName);
}
public void SetEnvironment(String Environment) {
this.Environment = Environment;
}
public String getAppName() {
return AppName;
}
public String getType() {
return Type;
}
public List<String> getServerName() {
return ServerNames;
}
public String getEnvironment() {
return Environment;
}
}
Your exception is being caused because you didn't initialize ServerNames in your Project class. Try to initialize it as follows and rerun:
final protected List<String> ServerNames = new ArrayList<String>();
If your xml was created using an xsd schema, you could instead use JAXB to create classes for it, using the xjc tool. That should make your life a bit easier.

Categories

Resources