Java Dom getAttribute - java

im trying to parse xml file, but it wont print attribut value. I dont know how to get attribute typ from phone
try { String subor = "Noviny.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(subor);
System.out.println("----------------\n");
NodeList nodelist = document.getElementsByTagName("Author");
NodeList nodelist1 = document.getElementsByTagName("Article");
for(int i = 0; i < nodelist.getLength(); i++) {
Node uzol = nodelist.item(i);
if (uzol.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) uzol;
System.out.println("Id:" + element.getElementsByTagName("Id").item(0).getTextContent() + "\n");
System.out.println("Name:" + element.getElementsByTagName("Name").item(0).getTextContent() + "\n");
System.out.println("Email:" + element.getElementsByTagName("Email").item(0).getTextContent() + "\n");
System.out.println("typ: " + element.getAttribute("typ") + "\n");
System.out.println("phone:" + element.getElementsByTagName("phone").item(0).getTextContent() + "\n");
System.out.println("typ: " + element.getAttribute("typ") + "\n");
System.out.println("sal: " + element.getElementsByTagName("sal").item(0).getTextContent() + "\n");
}
catch (Exception e) {
e.printStackTrace();
}
and xml
<Noviny>
<Author>
<Id>1</Id>
<Name>first</Name>
<Email>first#gmail.com</Email>
<phone typ="mobil">09443916565</phone>
<sal>500</sal>
</Author>
<Author>
<Id>2</Id>
<Name>second</Name>
<Email>second#gmail.com</Email>
<phone typ="pevna">094415665465</phone>
<sal>1000</sal>
</Author>
and one more thing:ňIs it possible to just print (System.out.println) tree representation of xml file?
thank you

You're currently asking for the typ attribute of the <Author> element (twice, for some reason). You should fetch the <phone> element (which you're already doing) and then ask for the typ attribute of that element, rather than the <Author> element.

Related

NOAA XML Parsing in Java - getting the latitude and longitude element

I am trying to parse this particular latitude and longitude string into my XML Parser written in Java, but I am not sure what element to use, as point does not having a closing />
<data>
<location>
<location-key>point1</location-key>
<point latitude="39.95" longitude="-75.17"/>
</location>
<moreWeatherInformation applicable-location="point1">
http://forecast.weather.gov/MapClick.php?textField1=39.95&textField2=-75.17
</moreWeatherInformation>
<time-layout time-coordinate="local" summarization="none">
<layout-key>k-p24h-n1-1</layout-key>
<start-valid-time>2013-10-30T08:00:00-04:00</start-valid-time>
<end-valid-time>2013-10-30T20:00:00-04:00</end-valid-time>
</time-layout>
<time-layout time-coordinate="local" summarization="none">
<layout-key>k-p24h-n2-2</layout-key>
<start-valid-time>2013-10-29T20:00:00-04:00</start-valid-time>
<end-valid-time>2013-10-30T09:00:00-04:00</end-valid-time>
<start-valid-time>2013-10-30T20:00:00-04:00</start-valid-time>
<end-valid-time>2013-10-31T09:00:00-04:00</end-valid-time>
</time-layout>
<parameters applicable-location="point1">
<temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n1-1">
<name>Daily Maximum Temperature</name>
<value>64</value>
</temperature>
<temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n2-2">
<name>Daily Minimum Temperature</name>
<value>44</value>
<value>52</value>
</temperature>
</parameters>
</data>
Using the code below, I get the following output.
try {
File newXMLFile = new File("H:\\520\\SOAP Data Acquisition\\testNOAA.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(newXMLFile);
doc.getDocumentElement().normalize();
System.out.println("Title: " + doc.getElementsByTagName("title").item(0).getTextContent());
System.out.println("Field: " + doc.getElementsByTagName("field").item(0).getTextContent());
System.out.println("category: " + doc.getElementsByTagName("category").item(0).getTextContent());
System.out.println("Date Created: " + doc.getElementsByTagName("creation-date").item(0).getTextContent());
NodeList nList = doc.getElementsByTagName("data");
for(int i = 0; i < nList.getLength(); i++){
Node nNode = nList.item(i);
System.out.println("\nCurrent Element: " + nNode.getNodeName());
if(nNode.getNodeType() == Node.ELEMENT_NODE){
Element eElement = (Element) nNode;
System.out.println("Location: " + eElement.getAttribute("point"));
System.out.println("Start Time: "+eElement.getElementsByTagName("start-valid-time").item(0).getTextContent());
System.out.println("End Time: "+eElement.getElementsByTagName("end-valid-time").item(0).getTextContent());
System.out.println("");
}
}
}catch(Exception e){
e.printStackTrace();
}
Output:
Title: NOAA's National Weather Service Forecast Data
Field: meteorological
category: forecast
Date Created: 2013-10-29T14:42:58Z
Current Element: data
Location:
Start Time: 2013-10-30T08:00:00-04:00
End Time: 2013-10-30T20:00:00-04:00
For the relevant part of your code try this instead. It's assuming there's only going to be one "point" in the entire "data" tag (or at least, that you're only interested in the first "point"), but it works for what you have posted.
NodeList nList = doc.getElementsByTagName("data");
for(int i = 0; i < nList.getLength(); i++){
Node nNode = nList.item(i);
System.out.println("\nCurrent Element: " + nNode.getNodeName());
if(nNode.getNodeType() == Node.ELEMENT_NODE){
Element eElement = (Element) nNode;
Element point = (Element)(eElement.getElementsByTagName("point").item(0));
System.out.println("Location: " + point.getAttribute("latitude") + ", " + point.getAttribute("longitude"));
System.out.println("Start Time: "+eElement.getElementsByTagName("start-valid-time").item(0).getTextContent());
System.out.println("End Time: "+eElement.getElementsByTagName("end-valid-time").item(0).getTextContent());
System.out.println("");
}
}

XML parsing. How can I get child's child?

I'm trying to parse this XML in Java:
<entities>
<entity name="product_section" id="1">
<product_type>3</product_type>
<section_type>1</section_type>
<name>Empresa</name>
<description>d</description>
<position>1</position>
<align>left</align>
<files section_id="1">
<ico id="ico_1" type="normal" src="sections/1/icons/ico.png"></ico>
<ico id="ico_2" type="hover" src="sections/1/icons/ico.png"></ico>
<ico id="ico_3" type="active" src="sections/1/icons/ico.png"></ico>
<img id="img_1" type="normal" src="sections/1/img/pestanya.png"></img>
<img id="img_2" type="hover" src="sections/1/img/pestanya-hover.png"></img>
<img id="img_3" type="active" src="sections/1/img/pestanya-active.png"></img>
<background id="background_1" type="background" position="1" src="sections/1/background/bg1.png"></background>
<background id="background_2" type="background" position="2" src="sections/1/background/bg2.png"></background>
<background id="background_3" type="background" position="3" src="sections/1/background/bg3.png"></background>
</files>
</entity>
But I just achieved to loop through Entities, getting all Entity and each <product_type>, <section_type>, etc.
But I want to loop through files too.
This is my implementation so far:
try {
File contingut = new File("xmlfile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(contingut);
doc.getDocumentElement().normalize();
System.out.println("root of xml file " + doc.getDocumentElement().getNodeName());
//loop a cada entity
NodeList nodes = doc.getElementsByTagName("entity");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
Element element = (Element) node;
System.out.println("product_type: " + getValue("product_type", element));
System.out.println("section_type: " + getValue("section_type", element));
System.out.println("name: " + getValue("name", element));
System.out.println("description: " + getValue("description", element));
System.out.println("position: " + getValue("position", element));
System.out.println("align: " + getValue("align", element));
}
} catch (Exception e){
e.printStackTrace();
}
getValue function is:
private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
I've done lot of google search, and all I find are "simple" examples, with a parent, and a child, but not child's child.
Any help would be appreciated.
At first one suggestion:
check element type after this Element element = (Element) node;
use this code or something like this :
if (element.getNodeType() == Element.ELEMENT_NODE) { // do smth}
and answer to your question:
You can simply rewrite you code. after you create element you can get all it's child elements by using element.getChildNodes();
it gives you all child tags. After that you write simple for loop where you get each node element from node list like this :
NodeList nodes = element.getChildNodes();
for(int i =0; i < nodes.getLength(); i++){
Element child = (Element) nodes.item(i);
if(child.getNodeType() == Element.ELEMENT_NODE){
String tagName = child.getTagName();
if(!tagName.equals("files")){
System.out.println(tagName + " : " + child.getTextContent());
}else{
NodeList filesChilds = child.getChildNodes();
for(int j = 0; j < filesChilds.getLength(); j++){
//and like above
}
}
}
}

Why is this Java method not returning all XML Elements?

I'm working on reading DMX Values from an XML document. The method only returns one node from the element that I'm trying to pull from but there should be 512.
Here is the method:
public static void readXML(int cueNo){
try {
File fXmlFile = new File(MixWindow.Globals.fileLoc);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Cue");
System.out.println("-----------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = (Node) nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Cue Name : " + getTagValue("Cue_Name", eElement));
System.out.println("Cue Number : " + getTagValue("Cue_Number", eElement));
//System.out.println("Nick Name : " + getTagValue("nickname", eElement));
//System.out.println("Salary : " + getTagValue("salary", eElement));
}
}
NodeList nListII = doc.getElementsByTagName("DMX");
//nListII = doc.getElementsByTagName("DMX");
System.out.println("-----------------------");
int length = nListII.getLength();
System.out.println("DMX Length: " + length);
for (int tempII = 0; tempII < nListII.getLength(); tempII++) {
Node nNodeII = (Node) nListII.item(tempII);
if (nNodeII.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNodeII;
System.out.println("DMX Chnl: " + getTagValue("DMX_Chnl", eElement));
System.out.println("DMX Val: " + getTagValue("DMX_Val", eElement));
//System.out.println("Nick Name : " + getTagValue("nickname", eElement));
//System.out.println("Salary : " + getTagValue("salary", 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();
}
}
Here is a portion of the XML file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ShowFile>
<Cue>
<Cue_Name>stuff and junk</Cue_Name>
<Cue_Number>1</Cue_Number></Cue>
<DMX>
<DMX_Chnl>1</DMX_Chnl>
<DMX_Val>0</DMX_Val>
<DMX_Chnl>2</DMX_Chnl>
<DMX_Val>0</DMX_Val>
<DMX_Chnl>3</DMX_Chnl>
<DMX_Val>0</DMX_Val>
<DMX_Chnl>4</DMX_Chnl>
<DMX_Val>0</DMX_Val>
......
<DMX_Chnl>512</DMX_Chnl>
<DMX_Val>0</DMX_Val>
System.out created this:
Cue Name : stuff and junk
Cue Number : 1
-----------------------
DMX Length: 1
DMX Chnl: 1
DMX Val : 0
What am I doing wrong?
shortened xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><ShowFile><Cue><Cue_Name>Stuff and Junk</Cue_Name><Cue_Number>1</Cue_Number></Cue><DMX><DMX_Chnl>1</DMX_Chnl><DMX_Val>0</DMX_Val><DMX_Cue>1</DMX_Cue><DMX_Chnl>2</DMX_Chnl><DMX_Val>0</DMX_Val><DMX_Cue>1</DMX_Cue><DMX_Chnl>3</DMX_Chnl><DMX_Val>0</DMX_Val><DMX_Cue>1</DMX_Cue><DMX_Chnl>4</DMX_Chnl></DMX></ShowFile>
added loop:
if (nNodeII.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNodeII;
NodeList childNodes = nNodeII.getChildNodes();
String result = new String();
for (int i = 0; i < childNodes.getLength(); i++) {
Node node = childNodes.item(i);
String dcName = node.getNodeName();
String dcVal = node.getNodeValue();
System.out.println("DMX stuff: " + dcName + " " + dcVal);
}
}
Java and the XML parser behave correctly.
But you expect the document to have a different structure than it actually does! So you are looking in the wrong place for nodes, and thus not finding nodes there.
There is only one DMX element. You want to enumerate the children, not the DMX elements.
(i.e. you want to have DMX_Chnl elements, and these are not each wrapped in a separate DMX node)

XPath and Java with Repeated Tags

I'm having some trouble parsing an XML file in Java. The file takes the form:
<root>
<thing>
<name>Thing1</name>
<property>
<name>Property1</name>
</property>
...
</thing>
...
</root>
Ultimately, I would like to convert this file into a list of Thing objects, which will have a String name (Thing1) and a list of Property objects, which will each also have a name (Property1).
I've been trying to use xpaths to get this data out, but when I try to get just the name for 'thing', it gives me all of the names that appear in 'thing', including those of the 'property's. My code is:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(filename);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression thingExpr = xpath.compile("//thing");
NodeList things = (NodeList)thingExpr.evaluate(dom, XPathConstants.NODESET);
for(int count = 0; count < things.getLength(); count++)
{
Element thing = (Element)things.item(count);
XPathExpression nameExpr = xpath.compile(".//name/text()");
NodeList name = (NodeList) nameExpr.evaluate(thing, XPathConstants.NODESET);
for(int i = 0; i < name.getLength(); i++)
{
System.out.println(name.item(i).getNodeValue());
}
}
Can anyone help? Thanks in advance!
You could try something like...
public class TestXPath {
public static void main(String[] args) {
String xml =
"<root>\n"
+ " <thing>\n"
+ " <name>Thing1</name>\n"
+ " <property>\n"
+ " <name>Property1</name>\n"
+ " </property>\n"
+ " <property>\n"
+ " <name>Property2</name>\n"
+ " </property>\n"
+ " <property>\n"
+ " <name>Property3</name>\n"
+ " </property>\n"
+ " <property>\n"
+ " <name>Property4</name>\n"
+ " </property>\n"
+ " <property>\n"
+ " <name>Property5</name>\n"
+ " </property>\n"
+ " </thing>/n"
+ " <NoAThin>\n"
+ " <name>Thing2</name>\n"
+ " <property>\n"
+ " <name>Property1</name>\n"
+ " </property>\n"
+ " <property>\n"
+ " <name>Property2</name>\n"
+ " </property>\n"
+ " <property>\n"
+ " <name>Property3</name>\n"
+ " </property>\n"
+ " <property>\n"
+ " <name>Property4</name>\n"
+ " </property>\n"
+ " <property>\n"
+ " <name>Property5</name>\n"
+ " </property>\n"
+ " </NoAThin>/n"
+ "</root>";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
Document dom = db.parse(bais);
XPath xpath = XPathFactory.newInstance().newXPath();
// Find the "thing" node...
XPathExpression thingExpr = xpath.compile("/root/thing");
NodeList things = (NodeList) thingExpr.evaluate(dom, XPathConstants.NODESET);
System.out.println("Found " + things.getLength() + " thing nodes...");
// Find the property nodes of thing
XPathExpression expr = xpath.compile("property");
NodeList nodes = (NodeList) expr.evaluate(things.item(0), XPathConstants.NODESET);
System.out.println("Found " + nodes.getLength() + " thing/property nodes...");
// Find all the property "name" nodes under thing
expr = xpath.compile("property/name");
nodes = (NodeList) expr.evaluate(things.item(0), XPathConstants.NODESET);
System.out.println("Found " + nodes.getLength() + " name nodes...");
System.out.println("Property value = " + nodes.item(0).getTextContent());
// Find all nodes that have property nodes
XPathExpression exprAll = xpath.compile("/root/*/property");
NodeList nodesAll = (NodeList) exprAll.evaluate(dom, XPathConstants.NODESET);
System.out.println("Found " + nodesAll.getLength() + " property nodes...");
} catch (Exception exp) {
exp.printStackTrace();
}
}
}
Which will give you an output of something like
Found 1 thing nodes...
Found 5 thing/property nodes...
Found 5 name nodes...
Property value = Property1
Found 10 property nodes...
How about "//thing/name/text()" ?
The double slashes you have now before name mean "anywhere in the tree, not necessarily direct child nodes".
Use these XPath expressions:
//thing[name='Thing1']
this selects any thing element in the XML document, that has a name child whose string value is "Thing1".
also use:
//property[name='Property1']
this selects any property element in the XML document, that has a name child whose string value is "Property1".
Update:
To get all text nodes, each containing a string value of a thing element, just do:
//thing/text()
In XPath 2.0 one can get a sequence of the strings themselves, using:
//thing/string(.)
This isn't possible with a single XPath expression, but one can get the string value of a particular (the n-th)thing element like this:
string((//thing)[$n])
where $n must be substituted with a specific number from 1 to count(//thing) .
So, in your prograaming language, you can first determine cnt by evaluating this XPath expression:
count(//thing)
And then in a loop for $n from 1 to cnt dynamically produce the xpath expression and evaluate it:
string((//thing)[$n])
Exactly the same goes for obtaining all the values for property elements.

reading from XMl file in java

I have a simple XML file
<requirements>
<requirement>
<name> SwitchON</name>
<id>1</id>
<text>The Light shall turn on when the Switch is on.</text>
</requirement>
<requirement>
<name>SwitchOFF</name>
<id>2</id>
<text>The Light shall turn off when the Switch is off.</text>
</requirement>
<requirement>
<name>Lightbulb</name>
<id>3</id>
<text>The Light bulb shall be connected </text>
</requirement>
<requirement>
<name>Power</name>
<id>4</id>
<text>The Light shall have the power supply</text>
</requirement>
</requirements>
I am trying to show the information in this file in a table model.
I have a method (readFromXMl) that reads the XML file and returns a table model.
public static RequirementTable readFromXMl(String fileName) {
RequirementTable T = new RequirementTable();
Requirement R = new Requirement();
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File(fileName));
doc.getDocumentElement().normalize();
NodeList listOfRequirements = doc.getElementsByTagName("requirement");
int test = listOfRequirements.getLength();
System.out.println("Total no of people : " + test);
for (int i = 0; i < listOfRequirements.getLength(); i++) {
Node RequirementNode = listOfRequirements.item(i);
if (RequirementNode.getNodeType() == Node.ELEMENT_NODE) {
Element RequirementElement = (Element) RequirementNode;
NodeList IdList = RequirementElement.getElementsByTagName("id");
Element IdElement = (Element) IdList.item(0);
NodeList textIdList = IdElement.getChildNodes();
R.setId(Integer.parseInt(textIdList.item(0).getNodeValue()));
NodeList DescriptionList = RequirementElement.getElementsByTagName("text");
Element DescriptionElement = (Element) DescriptionList.item(0);
NodeList textDescriptionList = DescriptionElement.getChildNodes();
R.setText(textDescriptionList.item(0).toString());
NodeList NameList = RequirementElement.getElementsByTagName("name");
Element NameElement = (Element) NameList;
NodeList textNameList = NameElement.getChildNodes();
if (textNameList.item(0).toString().equals("SwitchON")) {
T.addRequirement((SwitchOnReq)R);
} else if (textNameList.item(0).toString().equals("SwitchOFF")) {
T.addRequirement((SwitchOFFReq)R);
} else if (textNameList.item(0).toString().equals("LightBulb")) {
T.addRequirement((BulbRequirement)R);
} else if (textNameList.item(0).toString().equals("Power")) {
T.addRequirement((PowerRequirement)R);
}
}
}
} catch (SAXParseException err) {
System.out.println("** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
} catch (SAXException e) {
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
return T;
}
However in this line I am getting an error which says the the pointer is null
Element IdElement = (Element) IdList.item(0); IdElement is null!!
Instead of all the looping and other xml ugliness, let me suggest a little helper method:
private static String getNodeValue(Node n, String path)
throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
return (String) xpath.evaluate(path, n, XPathConstants.STRING);
}
Use like this:
for (int i = 0; i < listOfRequirements.getLength(); i++) {
Node RequirementNode = listOfRequirements.item(i);
System.out.println("name:" + getNodeValue(RequirementNode, "name"));
System.out.println("id:" + getNodeValue(RequirementNode, "id"));
System.out.println("text:" + getNodeValue(RequirementNode, "text"));
...
to get all the values and set your requirements.

Categories