XPath Trouble with getting attributes - java

I'm having a bit of trouble with some XML in Java. The following is the result of an API call to EVE Online. How can I get the "name" and "characterID" for each row?
Frankly I just have no idea where to start with this one, so please don't ask for extra information. I just gotta know how to get those attributes.
<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="1">
<currentTime>2007-12-12 11:48:50</currentTime>
<result>
<rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID">
<row name="Mary" characterID="150267069"
corporationName="Starbase Anchoring Corp" corporationID="150279367" />
<row name="Marcus" characterID="150302299"
corporationName="Marcus Corp" corporationID="150333466" />
<row name="Dieinafire" characterID="150340823"
corporationName="Center for Advanced Studies" corporationID="1000169" />
</rowset>
</result>
<cachedUntil>2007-12-12 12:48:50</cachedUntil>
</eveapi>

Try
/eveapi/result/rowset/row/#name
and
/eveapi/result/rowset/row/#key

Related

Add an node to an xml when the same node is present in the xml as an empty tag

<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE>
<STATUS>
<SERVERTIME><![CDATA[15319123123262]]></SERVERTIME>
<TYPE><![CDATA[getData]]></TYPE>
<RESPONSE_VERSION><![CDATA[1]]></RESPONSE_VERSION>
<DATA_VERSION><![CDATA[1]]></DATA_VERSION>
<VALUE><![CDATA[1]]></VALUE>
<ERRORS />
<WARNINGS />
</STATUS>
<DATA>
<CONTENT RECORDID="f2e110aa8ca24aewq929d26b9fcf3108962">
<METARESOURCEPATH><![CDATA[sites/xyz/content/meta/INFORMATION/0/INF427/]]></METARESOURCEPATH>
<REVIEW_TIMESTAMP><![CDATA[2018-07-20 15:36:00 IST]]></REVIEW_TIMESTAMP>
<REVIEW_TIMESTAMP_MILLIS><![CDATA[1532081160000]]></REVIEW_TIMESTAMP_MILLIS>
<VIEWS>
<VIEW>
<NAME><![CDATA[xyz]]></NAME>
<REFERENCE_KEY><![CDATA[xyz]]></REFERENCE_KEY>
<GUID><![CDATA[6d024478feb441231661f163de62d6e]]></GUID>
<OBJECTID><![CDATA[001]]></OBJECTID>
<PARENTID />
<CHILDCOUNT><![CDATA[8]]></CHILDCOUNT>
</VIEW>
</VIEWS>
<CATEGORIES />
</CONTENT>
</DATA>
</RESPONSE>
I need to add an element to CATEGORIES WHICH IS OF THE FORMAT.
When ever i try to add a new category it does not accept it because it finds a duplicate categories tag. But when i try to remove that empty tag , that is also not accepted because getelementbyname only returns null for categories.
<CATEGORIES>
<CATEGORY>
<NAME><![CDATA[xyz]]></NAME>
<REFERENCE_KEY><![CDATA[xyz]]></REFERENCE_KEY>
<GUID><![CDATA[6d024478feb441231661f163de62d6e]]></GUID>
<OBJECTID><![CDATA[001]]></OBJECTID>
<PARENTID />
<CHILDCOUNT><![CDATA[8]]></CHILDCOUNT>
</CATEGORY>
</CATEGORIES>
How can this be done with max efficiency ?

Parse an XML into Java - MetaData Format

I saw some xml parsing in java but I really don't know how I can apply it to my code.
Here is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xml:base="https://adomain.com">
<id>https://sharepoint.mydomain/aFile)</id>
<category term="SP.File" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2015-05-18T07:13:18Z</updated>
<author>
Bla Bla<name />
</author>
<content type="application/xml">
<m:properties>
<d:CheckInComment />
<d:CheckOutType m:type="Edm.Int32">2</d:CheckOutType>
<d:ContentTag>{63FD2CFA-D223-405B-86B3-D59B34ECEBBE},3,1</d:ContentTag>
<d:CustomizedPageStatus m:type="Edm.Int32">0</d:CustomizedPageStatus>
<d:ETag>"{63FD2CFA-D223-405B-86B3-D59B34ECEBBE},3"</d:ETag>
<d:Exists m:type="Edm.Boolean">true</d:Exists>
<d:Length m:type="Edm.Int64">638367</d:Length>
<d:Level m:type="Edm.Byte">2</d:Level>
<d:MajorVersion m:type="Edm.Int32">0</d:MajorVersion>
<d:MinorVersion m:type="Edm.Int32">1</d:MinorVersion>
<d:Name>aName.pdf</d:Name>
<d:ServerRelativeUrl>/mydomain.com/afile</d:ServerRelativeUrl>
<d:TimeCreated m:type="Edm.DateTime">2014-09-03T15:30:22Z</d:TimeCreated>
<d:TimeLastModified m:type="Edm.DateTime">2014-09-03T15:30:25Z</d:TimeLastModified>
<d:Title />
<d:UIVersion m:type="Edm.Int32">1</d:UIVersion>
<d:UIVersionLabel>0.1</d:UIVersionLabel>
</m:properties>
</content>
</entry>
I am trying to get the metadata of a file from SharePoint which is displayed in xml format.
How can I get the data which is inside the content and also the title and the author like this:
Author BlaBla
Title Bla
Type application/xml
TimeLastModified xx/xx/xxxx
The easiest way to parse XML files is the DOM parser. You can find the documentation here and a few tutorials here and here.
Also, a related question in stackoverflow here.
You can use Jaxb, now a days it is used for parsing purpose very effectively, Converting XML to JAVA called UnMarshalling http://www.javatpoint.com/jaxb-tutorial

Stuck with JDOM Parsing?

I have a complex JDOM element like following (A), I need to change the structure like (B), for working on JAXB (Using with already existing classes,
only thing I can do is changing the structure of xml), Can I able to do this using JDOM api?
As I am a beginer in java, it is very difficult for me, if anyone point-out a solution, it is very much helpful for me
Existing element (A)
<DETAILS>
<ROWSET name="OPTIONS">
<ROW num="1">
<Uniqueno>1000</Uniqueno>
<ROWSET name="SUSPENCE">
<ROW num="1">
<Uniqueno>1001</Uniqueno>
<ROWSET name="PERSONS">
<ROW num="1">
<Name>60821894</Name>
<Age>44</Age>
</ROW>
<ROW num="2">
<Name>60821894</Name>
<Age>44</Age>
</ROW>
</ROWSET>
<ROWSET name="PERSONS">
<ROW num="1">
<Name>60821894</Name>
<Age>55</Age>
</ROW>
<ROW num="2">
<Name>60821894</Name>
<Age>55</Age>
</ROW>
<ROW num="3">
<Name>60821894</Name>
<Age>55</Age>
</ROW>
</ROWSET>
</ROW>
</ROWSET>
</ROW>
</ROWSET>
</DETAILS>
Required element (B)
<DETAILS>
<OPTIONS>
<Uniqueno>1000</Uniqueno>
<SUSPENCE>
<Uniqueno>1001</Uniqueno>
<PERSONS>
<Name>60821894</Name>
<Age>44</Age>
<Name>60821894</Name>
<Age>44</Age>
</PERSONS>
<PERSONS>
<Name>60821894</Name>
<Age>55</Age>
<Name>60821894</Name>
<Age>55</Age>
<Name>60821894</Name>
<Age>55</Age>
</PERSONS>
</SUSPENCE>
</OPTIONS>
</DETAILS>
May I suggest to use XSLT instead. Much easier. Start with something like this
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="DETAILS/ROWSET[#name='OPTIONS']">
<DETAILS>
<OPTIONS>
<xsl:apply-templates />
</OPTIONS>
</DETAILS>
</xsl:template>
<xsl:template match="ROW">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Looking at the xmls, these are two entirely different xmls.You need to build a xml structure similar to B dyanamically.For this, the following link will help you.
http://www.ibm.com/developerworks/java/library/j-jdom/
Hope this will help you.
You have been asking essentially the same question multiple times.
Remove XML attribute using JDOM API?
Issues in Parsing XML
If you have not yet been able to get the previous questions right, you need to take a step back and work with more basic examples before you ramp up to doing multiple element moves.
While I agree with forty-two that XSL will be a better solution in the long run, I don't think you are in a place yet where that will make things easier (for you). If you have JDOM Elements available with your data, you should figure out your Java Debugger, and inspect the Elements as you add and remove them. You need to 'play' a bit to get a better understaning of how Java, XML, and JDOM work. Right now you are asking a whole bunch of related questions that show a basic misunderstanding of what in effect are 'foundation' concepts. You need to get those foundations right before you tackle these more complex concepts.
How about you start with something simple:
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
Document doc = new Document();
Element root = new Element("DETAILS");
doc.addContent(root);
xout.output(System.out, doc);
Element row = new Element("ROW");
root.addContent(row);
xout.output(System.out, doc);
row.detach();
xout.output(System.out, doc);
You can use the above to see how content is added, and detached from JDOM content.
Then, when you have that figured out, you can put it in loops, scans, etc. so that you can detach, and re-add content from other places in the Document hierarchy.

Can I format data that is to be written in CSV file using java

I have some code to write data into a CSV file, but it writes data into a CSV
without formatting it properly. I want to bold some specific text. Is that possible?
CSV is just a plain text format, so you can't do any formatting.
If you want formatting, consider using an Excel library such as Apache POI or Jasper Reports. (Of course, then you end up with an excel file rather than a CSV, so depending on your situation that may or may not be appropriate)
As a side note, there are some strange nuances to writing CSV (such as making sure quotes, commas etc are properly escaped). There's a nice lightweight library I've used called Open CSV that might make your life easier if you choose to just stick with plain old CSV.
CSV is a plain text file format, you can not use any text effect.
Not that I am aware of, CSV is a plain text format.
If you are creating a csv, so that you can open it up in Excel, then I would suggest taking a look at the MS Excel XML format.
http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats#Excel_XML_Spreadsheet_example
An example would be as follows (taken from the wikipedia link, and this makes some text BOLD)
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Darl McBride</Author>
<LastAuthor>Bill Gates</LastAuthor>
<Created>2007-03-15T23:04:04Z</Created>
<Company>SCO Group, Inc.</Company>
<Version>11.8036</Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>6795</WindowHeight>
<WindowWidth>8460</WindowWidth>
<WindowTopX>120</WindowTopX>
<WindowTopY>15</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom" />
<Borders />
<Font />
<Interior />
<NumberFormat />
<Protection />
</Style>
<Style ss:ID="s21">
<Font x:Family="Swiss" ss:Bold="1" />
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="5"
x:FullColumns="1" x:FullRows="1">
<Row>
<Cell>
<Data ss:Type="String">Text in cell A1</Data>
</Cell>
</Row>
<Row>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Bold text in A2</Data>
</Cell>
</Row>
<Row ss:Index="4">
<Cell ss:Index="2">
<Data ss:Type="Number">43</Data>
</Cell>
</Row>
<Row>
<Cell ss:Index="2" ss:Formula="=R[-1]C/2">
<Data ss:Type="Number">21.5</Data>
</Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Print>
<ValidPrinterInfo />
<HorizontalResolution>600</HorizontalResolution>
<VerticalResolution>600</VerticalResolution>
</Print>
<Selected />
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>5</ActiveRow>
<ActiveCol>1</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook
I plain old regular CSV no. But there is no reason why you could not encode your data before writing it out... for example the HTML tags for bold is <b></b>. This would signal to you exactly which portions of the text are bold and being a from a well know standard is still human readable too. The main drawback is you have to parse your data after you read it :(
Something else to consider, since you are writing the data out why not write it out as comma separated values in RTF or some other format that does support bold etc? Normally CSV is plain text but there is no reason why you couldn't write it out another way. Just remember to read it back in the same format...

Dynamic xml filtering and transform (in Java)

I have an XML file that looks like
<?xml version='1.0' encoding='UTF-8'?>
<root>
<node name="foo1" value="bar1" />
<node name="foo2" value="bar2" />
</root>
I have a method
String processBar(String bar)
and I want to end up with
<?xml version='1.0' encoding='UTF-8'?>
<root>
<node name="foo1" value="processBar("bar1")" />
<node name="foo2" value="processBar("bar2")" />
</root>
Is there an easy way to do this? Preferably in Java. Note that the file is too large to safely load completely into memory. The data in the XML roughly arbitrary and processBar may be complex, so I don't want to use regular expressions.
Assuming you mean replacing the attribute values with the result of calling processBar on said attribute values...
Use the JDK's XSLT API to run the following:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="http://xml.apache.org/xalan/java"
extension-element-prefixes="java">
<xsl:template match="/root/node/#value">
<xsl:attribute name="value">
<xsl:value-of select="java:com.example.yourclass.processBar(string(.))"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
This uses the Xalan-Java extensions and assumes a static method. You can get an instance of an object and store it in an xsl:variable, like this:
<xsl:variable name="frobber" select="java:com.example.Frobber.new()"/>
<xsl:value-of select="java:processBar($frobber, string(.))"/>
Or somesuch.
This only works with Xalan, but since that's the XSLT processor distributed with the JDK, I doubt it will be onerous to use Xalan.
you can either parse the whole thing in a java xml parser OR just get the file content into a string and then do a regexp replace on it (using i.e. http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29)

Categories

Resources