aspose file tools*
The moose likes XML and Related Technologies and the fly likes Search and Replace tag (Not its Value) in an XML file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Search and Replace tag (Not its Value) in an XML file." Watch "Search and Replace tag (Not its Value) in an XML file." New topic
Author

Search and Replace tag (Not its Value) in an XML file.

Manzoor Elahi
Greenhorn

Joined: May 11, 2010
Posts: 11
Hi All,

I have an XML file, say RESULT.XML Which has about 30000 Lines (yes Thirty Thousand ). For some reason i need to replace an existing tag with a different tag(not the value inside the tag, But the tag itself) . I am facing a challenge. I will explain it as below.

I have a csv file which has 2 values as given below
<VALUE A> , <VALUE B>

where <VALUE A> is an existing node in the RESULT.XML and <VALUE B> is the node that should replace <VALUE A> in the RESULT.XML

i have 500 such entries in the file.

Now i need to write a program to read the csv file, Search <VALUE A> in the RESULT.XML and Replace it with <VALUE B> of the csv file.

please let me know how do i start with this.
I really want to fix this issue.

Existing Node = <Product ID="role (contact) 3" UserTypeID="Product Number">

To be changed to = <Product ID= role (contact)3" UserTypeID="Product Number">


please reply if you need any further information

Thanks in Advance

Manzoor


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Read the XML into a DOM. Read the CSV data into a Map<String, String>. Then go through the DOM and examine every Element node. If its name is a key of the Map, change the name to be the value instead. When you're finished, save the DOM to a new document.
Manzoor Elahi
Greenhorn

Joined: May 11, 2010
Posts: 11
Thanks for the reply,

i have one more doubt,

Existing Node = <Product ID="role (contact) 3" UserTypeID="Product Number">

To be changed to = <Product ID= role (contact)3" UserTypeID="Product Number">

i have to change only the highlighted text, for that is replacing the full tag the only option or can i replace the attribute value

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42935
    
  68
Yes, it's possible to read and write attribute values in DOM (and other libraries like XOM, dom4j, JDOM, etc.). Check out the org.w3c.dom.Node and org.w3c.dom.Element interfaces.
Manzoor Elahi
Greenhorn

Joined: May 11, 2010
Posts: 11
HI ALL,

I tried alot, I am able to get the CSV values in a MAP with <String,String> format. I am not able to parse the xml file and replace its tags. Can someone please provide the code for it. I am new to XML technologies and i am really desperate to get this problem solved. any help would be really appreciated.

Thanks,
Manzoor
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42935
    
  68
What do you have so far? Post a relevant code excerpt that shows how you're handling the XML.
Manzoor Elahi
Greenhorn

Joined: May 11, 2010
Posts: 11
Here id the XML :
<STEP-ProductInformation ContextID="en_US" WorkspaceID="Main">
<Products>
<Product ID="Kulkarni" UserTypeID="Product Group" ParentID="Ramesh">
<Name>Kulkarni</Name>
<Product ID="Unilog - play identificat" UserTypeID="Product Group">
<Name>01001 Play Identification</Name>
<Values>
<Value AttributeID="SP_FIELD_NAME">IDENTIFICATION</Value>
</Values>
<Product ID="Unilog - Author Email" UserTypeID="Product Number">
<Name>02003 Author Email</Name>
<Values>
<Value AttributeID="SP_FIELD_NAME">AUTHOR_EMAIL</Value>
<Value AttributeID="SP_FIELD_VALUE">deborah bine/raleigh/ibm@ibmus</Value>
<Value AttributeID="SP_FIELD_INSTRUCTION">CDATA</Value>
</Values>
</Product>
<Product ID="Unilog - expire date" UserTypeID="Product Number">
<Name>02004 Expire Date</Name>
<Values>
<Value AttributeID="SP_FIELD_NAME">SP_EXPIRE_DATE</Value>
<Value AttributeID="SP_FIELD_VALUE">20120427</Value>
<Value AttributeID="SP_FIELD_INSTRUCTION">CDATA</Value>
</Values>
</Product> <Product ID="Unilog - published" UserTypeID="Product Number">
<Name>02005 Published (announce)</Name>
<Values>
<Value AttributeID="SP_FIELD_NAME">PPUBLISH</Value>
<Value AttributeID="SP_FIELD_VALUE">20100427</Value>
<Value AttributeID="SP_FIELD_INSTRUCTION">CDATA</Value>
</Values>
</Product>
<Product ID="Unilog - play name" UserTypeID="Product Number">
<Name>02002 Play Name</Name>
<Values>
<Value AttributeID="SP_FIELD_NAME">PLAY_NAME</Value>
<Value AttributeID="SP_FIELD_VALUE">Improve Checkout</Value>
<Value AttributeID="SP_FIELD_INSTRUCTION">CDATA</Value>
</Values>
</Product>
<Product ID="Unilog - pkey" UserTypeID="Product Number">
<Name>02001 Pkey</Name>
<Values>
<Value AttributeID="SP_FIELD_NAME">PKEY</Value>
<Value AttributeID="SP_FIELD_VALUE">modulenumberusen</Value>
<Value AttributeID="SP_FIELD_INSTRUCTION">CDATA</Value>
</Values>
</Product>
</Product>
</Product>
</Products>
</STEP-ProductInformation>

I am trying to change the highlighted text. which is occuring more than once ,

I have written the following code till now.


I dont know how to proceed next
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42935
    
  68
At a quick glance, it looks like the code extracts "Product" nodes, which you said are the ones you're interested in. (It also seems to do something with "Name" nodes - I'm not sure what that's about.)

The Node.getAttributes method provides access to all attributes of an element through a NamedNodeMap object; its getNamedItem and setNamedItem let you read and write attribute values.

catch (Exception e) {
}

This is almost never a good idea; at least print a message to the log file so you'll know there was a problem.
Manzoor Elahi
Greenhorn

Joined: May 11, 2010
Posts: 11
The following XML



As you can see there is a node called PRODUCT inside the PRODUCTS tag,and the PRODUCT tag itself has a child tag by name PRODUCT.

I need to replace all the Product tags with a separate value.



The Above gives me a nodelist of products. how do i extract the child "PRODUCT" Nodes from the nodelist
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42935
    
  68
There are "Product" elements nested inside of "Product" elements? Certainly not what I would call good XML design.

If you don't want to write DOM code to traverse the tree, you can use XPath to select just those nodes that are of interest; the XmlFaq links to an introductory article on that which will get you started. The XPath expression for this would be something like "//STEP-ProductInformation/Products/Product".
Manzoor Elahi
Greenhorn

Joined: May 11, 2010
Posts: 11
I managed to do it this way, Not the right way but can someone suggest what is "very" wrong in the below program so that i do it better next time

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42935
    
  68
What's "very wrong" with this is a) it completely punts on encodings (and will fail the first time a file comes along that's not in the platform default encoding), and b) that it replaces strings everywhere - not just in the attributes. Both these can easily be avoided by using proper XML APIs, thus making the code future-proof; why wouldn't you want to do that?

So, the next time you plan to ignore what folks here are trying to tell you, please let us know in advance so that we don't waste our time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Search and Replace tag (Not its Value) in an XML file.