I need to count specifics occurences on XML ("true" or "false") and get the results in a table
I try using a tXMLMap but so far without success.
Any idea where I can lookinf for?
My XML:
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer id="1">
<customerStatus vip="false" gold="false"/>
</customer>
<customer id="2">
<customerStatus vip="true" gold="false"/>
</customer>
<customer id="3">
<customerStatus vip="true" gold="true"/>
</customer>
</customers>
Wishes result:
vip
gold
2
1
Finnaly I succeed with:
tFileInputXml -> tXMLMap -> tAggregateRow -> tLogRow
Related
It is my first time for doing this.
Here is sample Input.xml file.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:Accounts xmlns:ns0="urn:text.com:accounts">
<Recordset>
<Record>
<FIELD1>123</FIELD1>
<FIELD2/>
<FIELD3/>
<FIELD4>2020</FIELD4>
<FIELD5/>
</Record>
<Record>
<FIELD6>89</FIELD6>
<FIELD7>098</FIELD7>
<FIELD8/>
</Record>
</Recordset>
</ns0:Accounts>
I want to output like this
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:Accounts xmlns:ns0="urn:text.com:accounts">
<Recordset>
<Record>
<FIELD1>123</FIELD1>
<FIELD2/>
<FIELD3/>
<FIELD4>2020</FIELD4>
<FIELD5/>
<FIELD6>89</FIELD6>
<FIELD7>098</FIELD7>
<FIELD8/>
</Record>
</Recordset>
</ns0:Accounts>
Here is java program:
NodeList child=doc.getElementsByTagName("Record");
for(int i=0;i<child.getLength();i++)
{
doc.renameNode(child.item(i),null,"");
}
To remove parent node like
Record
I've tried java DOM coding but didn't get.
Anyone help for this.
Thanks in advance
I have a XML File which I read data from XML File into Java Object and then insert these Java Objects into SQLite Database.
Then I modify my XML File and insert more data into it, how can I keep data between XML File and SQLite synchronized.
My XML File
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employees xmlns="https://www.journaldev.com/employee">
<Employee id="1">
<name>Pankaj</name>
<age>29</age>
<role>Java Developer</role>
<gender>Male</gender>
</Employee>
<Employee id="2">
<name>Lisa</name>
<age>35</age>
<role>Manager</role>
<gender>Female</gender>
</Employee>
</Employees>
Code to read XML File Employee in Java
https://anotepad.com/notes/qnn7nb2k
What you are looking for is a file watcher and since jdk1.7, you have the possibility to do this via WatchService.
A working example can be found in the docs here
I have some extremely large XML files that I need to process. I used to process them using Spark, but I am moving away from SQLDW and onto Snowflake, so I can no longer use Spark. In Spark, there was a concept of flattening XML files by providing a "rowTag" to a spark function. Let us say we have this persons.xml file:
<persons>
<person id="1">
<firstname>James</firstname>
<lastname>Smith</lastname>
<middlename></middlename>
<dob_year>1980</dob_year>
<dob_month>1</dob_month>
<gender>M</gender>
<salary currency="Euro">10000</salary>
<addresses>
<address>
<street>123 ABC street</street>
<city>NewJersy</city>
<state>NJ</state>
</address>
<address>
<street>456 apple street</street>
<city>newark</city>
<state>DE</state>
</address>
</addresses>
</person>
<person id="2">
<firstname>Michael</firstname>
<lastname></lastname>
<middlename>Rose</middlename>
<dob_year>1990</dob_year>
<dob_month>6</dob_month>
<gender>M</gender>
<salary currency="Dollor">10000</salary>
<addresses>
<address>
<street>4512 main st</street>
<city>new york</city>
<state>NY</state>
</address>
<address>
<street>4367 orange st</street>
<city>sandiago</city>
<state>CA</state>
</address>
</addresses>
</person>
</persons>
If I want to flatten this XML file to look like a CSV with headers firstname, lastname, middlename, dob_year, dob_month... etc, I would run a function that looks like this:
val df = spark.read
.format("com.databricks.spark.xml")
.option("rowTag", "person")
.load("persons.xml");
display(df);
By providing spark the rowTag person in the .option() function, we get a dataframe that looks like this:
_id addresses dob_month dob_year firstname gender lastname middlename salary
1 {"address":[{"city":"NewJersy","state":"NJ","street":"123 ABC street"},{"city":"newark","state":"DE","street":"456 apple street"}]} 1 1980 James M Smith {"_VALUE":10000,"_currency":"Euro"}
2 {"address":[{"city":"new york","state":"NY","street":"4512 main st"},{"city":"sandiago","state":"CA","street":"4367 orange st"}]} 6 1990 Michael M Rose {"_VALUE":10000,"_currency":"Dollor"}
It's a little difficult to read, so here is an image to help...
Anyways, I was wondering how I could do this with Snowflake, if it is possible? I would like to avoid pre-processing my xml file if possible.
Remember, these files are large. 1Gb+. There is also no guarantee that the files will have the rowTag in the beginning or near the beginning - it could be several hundred lines down the file.
Some ideas for you:
On load, use STRIP_OUTER_ELEMENT = TRUE to eliminate the PERSONS tag, and have each PERSON object land in it's own row. This simplifies the data and allows you to load larger files.
Flatten the XML to find all the paths. For example, select *
from my_table a, lateral flatten(input=>a.data, recursive=>true) b;
Translate the paths from the flatten notation into the field notation and build your query:
For example (assuming PERSONS outer tag removed):
select
data:"#id"::number id,
data:"$"[0]."$"::text first_name,
data:"$"[1]."$"::text last_name
from my_table;
Where data is your XML column.
Hope that helps.
UPDATE -- Sample XML to use with query above:
create or replace table my_table as
select parse_xml($1) as data
from values ('
<person id="1">
<firstname>James</firstname>
<lastname>Smith</lastname>
<middlename></middlename>
<dob_year>1980</dob_year>
<dob_month>1</dob_month>
<gender>M</gender>
<salary currency="Euro">10000</salary>
<addresses>
<address>
<street>123 ABC street</street>
<city>NewJersy</city>
<state>NJ</state>
</address>
<address>
<street>456 apple street</street>
<city>newark</city>
<state>DE</state>
</address>
</addresses>
</person>'),('
<person id="2">
<firstname>Michael</firstname>
<lastname></lastname>
<middlename>Rose</middlename>
<dob_year>1990</dob_year>
<dob_month>6</dob_month>
<gender>M</gender>
<salary currency="Dollor">10000</salary>
<addresses>
<address>
<street>4512 main st</street>
<city>new york</city>
<state>NY</state>
</address>
<address>
<street>4367 orange st</street>
<city>sandiago</city>
<state>CA</state>
</address>
</addresses>
</person>
');
It's the first time I'm using stackoverflow and I don't speak English perfectly so be nice please.
I'm using Jaxb in append mode like that
for (Document330 document : documents){
JAXBContext jContext = JAXBContext.newInstance(Document330Xml.class);
Marshaller m = jContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(document, fos);
}
And I have an output file like that:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DOCUMENT>
<MAILING>
<REF>M584</REF>
<LIBELLE>Mail Test 1</LIBELLE>
</MAILING>
</DOCUMENT>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DOCUMENT>
<MAILING>
<REF>M585</REF>
<LIBELLE>Mail Test 2</LIBELLE>
</MAILING>
</DOCUMENT>
but I want something like that :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DOCUMENTS>
<DOCUMENT>
<MAILING>
<REF>M584</REF>
<LIBELLE>Mail Test 1</LIBELLE>
</MAILING>
</DOCUMENT>
<DOCUMENT>
<MAILING>
<REF>M585</REF>
<LIBELLE>Mail Test 2</LIBELLE>
</MAILING>
</DOCUMENT>
</DOCUMENTS>
But it is possible that I have many XML. So I do not think the Unmarshaller is the best solution
Thanks for reading me
If I remind correctly, you need to create a Documents330Xml class, which can be marshalled (you can have a look at your Document330Xml class for reference). This class needs a list of Document330Xml as field.
If you then marshall the Documents330Xml class, you should get the desired XML.
I have a xml file(ABC.xml) and i need to duplicate only the
<Transaction>...</Transaction>
multiple times(more than 100000 times) keeping the Header and Trailer intact creating NEW.xml whose final size may go upto 1GB. Also i have to increment the Uniqueid for every transaction in sequence.
As i m new to xml, i have been searching to this the best possible way, and im confused.
Can anyone please help me with the best way to do it(using DOM or SAX) and some piece of code.
Also can you please give me some links about it.
ABC.xml
========
<?xml version="1.0" encoding="UTF-8"?>
<Header><Datetime><date>20130209</date><Time>01:12</Time></Datetime></Header>
<Transaction>
<Uniqueid>1230001</Uniqueid>
<Affiliate>
<Name>abc</Name>
<Address>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Address>
<Amount>123.00</Amount>
<Currency>USD</Currency>
<Purpose>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Purpose>
</Affiliate>
</Transaction>
<Trailer><TotalTransactions>1</TotalTransactions><TotalAmount>123<TotalAmount> </Trailer>
NEW.xml
=======
<?xml version="1.0" encoding="UTF-8"?>
<Header><Datetime><date>20130209</date><Time>01:12</Time></Datetime></Header>
<Transaction>
<Uniqueid>1230001</Uniqueid>
<Affiliate>
<Name>abc</Name>
<Address>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Address>
<Amount>123.00</Amount>
<Currency>USD</Currency>
<Purpose>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Purpose>
</Affiliate>
</Transaction>
<Transaction>
<Uniqueid>1230002</Uniqueid>
<Affiliate>
<Name>abc</Name>
<Address>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Address>
<Amount>123.00</Amount>
<Currency>USD</Currency>
<Purpose>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Purpose>
</Affiliate>
</Transaction>
<Trailer><TotalTransactions>2</TotalTransactions><TotalAmount>246<TotalAmount></Trailer>
It would help if your source XML were well-formed - it needs an outer wrapper element.
There are a number of XQuery processors available in Java, for example Saxon. Just execute the query
<doc>{doc/Header, for $i in 1 to 100000 return doc/Transaction, doc/Footer}</doc>
on the supplied input document, assuming <doc> as the outer wrapper element.