Here is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<productlist>
<product>
<title>1</title>
<price>30</price>
</product>
<product>
<title>2</title>
<price>9</price>
</product>
<product>
<title>3</title>
</product>
</productlist>
What I need to do is use Java DOM API to print out all the contents from XML file which contain "price" tag. Such things like this
title:1
price:30
title:2
price:9
In the org.w3c.dom documentation I only find hasAttribute(String name) method to check if this element have attribute but I can not find method like "hasTag(String name)" in documentation. I find this website http://www.java-forums.org/xml/62136-check-whether-xml-tag-exists-while-parsing-xml-using-dom-java.html but unfortunately I can not open the site. Hope you can help.
Just call getElementsByTagName and see if the returned list has any nodes (using NodeList.getLength()).
Related
I already have an xml which looks like the below one
<school>
<data>
<marks name="Peter"/>
<marks name="John"/>
</data>
</school>
I need to write a java code so that the xml is appended with an entry
<marks name="Dave"/>
I am using XStream library for XML parsing. I was wondering if the library allows jumping to a particular node directly using the index.
So for e.g.
<details>
<personal>
<basicInfo>
<firstName>John</firstName>
<lastName>Doe</lastName>
<phoneNumber>9999999999</phoneNumber>
<dateOfBirth>1990-01-01</dateOfBirth>
</basicInfo>
<address>
<street>random St.</street>
<city>City</city>
<stateProv>BC</stateProv>
<country>CA</country>
<postCode>12345</postCode>
</address>
</personal>
<personal>
<basicInfo>
<firstName>John2</firstName>
<lastName>Doe2</lastName>
<phoneNumber>9999999999</phoneNumber>
<dateOfBirth>1990-01-01</dateOfBirth>
</basicInfo>
<address>
<street>random St.2</street>
<city>City2</city>
<stateProv>BC2</stateProv>
<country>CA2</country>
<postCode>12345</postCode>
</address>
</personal>
</details>
For the XML above I would like to skip the first <personal>...</personal>
and only process the second node. Can I call it using an index.
XStream is a simple library to serialize objects to XML and back again.
I am not sure what you mean by process in this context, but if your POJO for serialization is set up correctly to contain a List of "personal" nodes. I don't see why you couldn't deserialize the XML and remove the unwanted node after the fact.
As far as I know, vtd-xml is the only XML parsing routine that natively offers indexing feature, called vtd+XML.
I have to output an XML file which can contain use amount of data, I am using DOM parser to write XML file. It is also possible to append data to an existing XML file.
My requirement is add data to the root element.
Is it possible to append data without reading the entire XML document (Not to load XML into memory)?
Example Data:
Current XML file:
<employees>
<employee>
<name>jon</name>
<age> 22</age>
<address> address1 </address>
</employee>
</employees>
Required file:
<employees>
<employee>
<name>jon</name>
<age> 22</age>
<address> address1 </address>
</employee>
<employee>
<name>jon1</name>
<age> 24</age>
<address> address2 </address>
</employee>
</employees>
It would be hard if you don't want to load entire XML into memory.
You can achieve this by manipulating raw String (substring, etc.) - but I don't recommend this.
Or you can try using SAX reader http://www.saxproject.org/apidoc/org/xml/sax/XMLReader.html which enables you to manipulate XMLs "on the go". (I'm sorry, although you can use SAX parsers to process XML without reading its whole content, you cannot edit with it)
EDIT:
On second though you can copy existing XML using SAX parser, and by adding event listener to e.g. root you can add a child. It might be good solution if your concern is memory (big xml file).
You could use DOM4j for doing that.
I am trying to copy a content from one xml to other xml using Xslt.
I need to copy content of file1
<?xml version="1.0"?>
<products author="Jesper">
<product>
<name>Delta</name>
<price>800</price>
<stock>
<price>13a</price>
</stock>
<place>Denmark</place>
</product>
</products>
to file 2. File2 has similar tags but order is jumbled,
<?xml version="1.0"?>
<products author="Jesper">
<product>
<stock>
<price>13d</price>
</stock>
<price>700</price>
<place>Copenhagen</place>
<name>Beta</name>
</product>
</products>
expected output
<products author="Jesper">
<product>
<stock>
<price>13a</price>
</stock>
<price>800</price>
<place>Denmark</place>
<name>Delta</name>
</product>
</products>
so basically I need to Iterate through file1 using for-each and then find the matching tag in file2 and copy the tag value. Not sure about an efficient way to do so ... Double iterating is inefficient. Any suggestion will be helpful.
This is a very broad question, but I'll try to give you some pointers that should get you started. You will probably want to use the doc() function to load the files since XSLT only allows you to iterate over a single "main" file. doc() loads a new file into a variable that you can apply templates to and so on. If you are concerned about the iteration performance, you should learn about xsl:key and the key() function, which build indexes that will help with that.
I am trying to write a program in java where in i can find all the xpath for the given xml.I found out the link on the internet xpath generator but it does not work when one element can repeat multipletimes for example if we have xml like the following :-
<?xml version="1.0" encoding="UTF-8"?>
<Report>
<Name>
<FirstName>A</FirstName>
<LastName>B</LastName>
<MiddleName>C</MiddleName>
</Name>
<Name>
<FirstName>D</FirstName>
<LastName>E</LastName>
<MiddleName>S</MiddleName>
</Name>
</Report>
It will produce xpaths :-
/Report/Name/firstname for both firstname nodes.
but the expected should be /Report/Name1/firstname and /Report/Name[2]/firstname
Any ideas?
I think you may have to do this yourself.
Using a SAX parser will make it straightforward. Just maintain a stack of the elements you encounter and a count so you can increment the indexes (/Report/Name[1], /Report/Name[2]) easily.