Solved

java dom - parsing/updating doc

Posted on 2008-10-09
4
153 Views
Last Modified: 2013-11-23
I've the following code and this html file.  I'm trying to locate the <head> tag and add text to it, however it isn't working.  Could someone please take a look at my code and tell me what is missing?  Thanks.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html"/>
<meta http-equiv="Content-Style-Type" content="text/css"/>
<title>a title</title>
</head>
<body>
<p>some text</p>
</body>
</html>

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

	    try

	    {

		DocumentBuilder db = dbf.newDocumentBuilder();

	    Document doc = db.parse(new File("test.html"));

	    

	

	String newinfo = "text to be added";
 

	 Element root = doc.getDocumentElement();

	    NodeList rootlist = root.getChildNodes();

	    

	    for(int i=0; i<rootlist.getLength(); i++) 

	    {

	        Element head = (Element)rootlist.item(i);

	         NodeList namelist = head.getChildNodes();

	        Text headtext = (Text)namelist.item(0);

	        String oldname = headtext.getData();

	        if(oldname.equals(newinfo)) 

	        {

	            Element head2 = (Element)namelist.item(1);

	            NodeList namelist2 = head.getChildNodes();

	            Text headtext2 = (Text)namelist.item(0);

	            headtext.setData(newinfo);

	        }

	    }

	    }

	    catch (Exception e)

	    {

	    	e.printStackTrace();

	    }

Open in new window

0
Comment
Question by:newbieal
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:wwnosal
Comment Utility
What exactly do you want to achieve.. Show example of an output..
0
 
LVL 4

Author Comment

by:newbieal
Comment Utility
The html file has a <head> tag.  I want to add text in between its beginning and ending tag.

<head> Some Text </head>
0
 
LVL 6

Accepted Solution

by:
wwnosal earned 500 total points
Comment Utility
Try like this:


DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ( );

    try {

      DocumentBuilder db = dbf.newDocumentBuilder ( );

      Document doc = db.parse ( new File ( "test.html" ) );
 

      String newinfo = "text to be added";
 

      Element root = doc.getDocumentElement ( );

      // NodeList list = doc.getElementsByTagName ( "head" );

      System.out.println ( root.getNodeName ( ) );

      NodeList list = root.getChildNodes ( );

      for ( int i = 0; i < list.getLength ( ); i++ ) {

        if ( list.item ( i ).getNodeName ( ).equals ( "head" ) ) {

          list.item ( i ).setTextContent ( newinfo );

        }

      }

      XMLSerializer serializer = new XMLSerializer ( );

      serializer.setOutputCharStream ( new java.io.FileWriter ( "test.html" ) );

      serializer.serialize ( doc );
 

    }

    catch ( Exception e ) {

      e.printStackTrace ( );

    }

Open in new window

0
 
LVL 4

Author Comment

by:newbieal
Comment Utility
Awesome, exactly what I needed, thank you so much!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
countX 22 68
array6 challenfge 6 62
JUnit 4 @Before and @BeforeClass differences 3 19
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now