bosssssa
bosssssa

Reputation: 11

XML Data extraction

<Filer>
<ID>123456789</ID>
<Name>
<BusinessNameLine1>Stackoverflow</BusinessNameLine1>
</Name>
<NameControl>stack</NameControl>
<USAddress>
<AddressLine1>123 CHERRY HILL LANE</AddressLine1>
<City>LA</City>
<State>CA</State>
<ZIPCode>90210</ZIPCode>
</USAddress>
</Filer>

Here I have a sample of xml code given to me. With this xml I need to grasp a certain attribute from this xml.

I simply need to extract all the <BusinessNameLine1> from the file. The issue is that this tag appears multiple times through out the file but I only need to extract it if it false in the <Filer> Tag.

I would do this with PHP but I am at work and I am not able to run php code due to not being able to install software on my computer. I can execute bash files however. The file is also extremely large so I can not put it in excel. I have no idea how to do this. I would appreciate some help or guidance on where to start.

Upvotes: 0

Views: 117

Answers (3)

Avinash Raj
Avinash Raj

Reputation: 174826

You could try this combined awk and sed commands,

$ awk -v RS='</Filer>' '/^<Filer>/ {gsub (/\n/," "); print}' file | sed -r 's/.*<BusinessNameLine1>([^<]*)<\/BusinessNameLine1>.*/\1/g'
Stackoverflow

Upvotes: 1

Kent
Kent

Reputation: 195229

xpath is your friend: there is xmllint tool, which could evaluate xpath

xmllint --xpath '//Filer//BusinessNameLine1/text()' yourXML

output:

Stackoverflow

test on an example with <Busn..> tag out of <Filer>:

kent$  cat t.xml
<root>
        <Trash>
                <BusinessNameLine1>trash</BusinessNameLine1>
        </Trash>
        <Filer>
                <ID>123456789</ID>
                <Name>
                        <BusinessNameLine1>Stackoverflow</BusinessNameLine1>
                </Name>
                <NameControl>stack</NameControl>
                <USAddress>
                        <AddressLine1>123 CHERRY HILL LANE</AddressLine1>
                        <City>LA</City>
                        <State>CA</State>
                        <ZIPCode>90210</ZIPCode>
                </USAddress>
        </Filer>
</root>

kent$  xmllint --xpath '//Filer//BusinessNameLine1/text()' t.xml    
Stackoverflow

Upvotes: 1

choroba
choroba

Reputation: 242168

Use a proper XML parser. For example, xsh:

open file.xml ;
ls //Filer//BusinessNameLine1 ;

Upvotes: 1

Related Questions